即使应用程序被杀死,声音也会继续播放

Sound keeps on playing even when app is killed

我正在制作一个有声音的测验应用程序 game.But 我发现即使关闭应用程序,声音也会继续播放。我两次使用 mediaplayer.

1.while 单击图像按钮。

2.on 改变问题。

问题每 15 秒更改一次,关闭应用程序后,声音每 15 秒播放一次。

 imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
              mediaPlayer.start();

        }
    });
    btnA.setOnClickListener(this);
    btnB.setOnClickListener(this);
    btnC.setOnClickListener(this);
    btnD.setOnClickListener(this);


    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.

}

@Override
protected void onResume()
{
    super.onResume();

    questionPlay = db.getMorseQuestionMode(mode);
    totalQuestion = questionPlay.size();

    mCountDown = new CountDownTimer(TIMEOUT, INTERVAL) {
        @Override
        public void onTick(long millisUntilFinished) {
            progressBar.setProgress(progressValue);
            progressValue++;

        }

        @Override
        public void onFinish() {
            mCountDown.cancel();
            showQuestion(++index);
        }
    };
   showQuestion(index);
}


private void showQuestion(int index) {

    if (index < totalQuestion) {
        thisQuestion++;
        txtQuestion.setText(String.format("%d/%d", thisQuestion, totalQuestion));
        progressBar.setProgress(0);
        progressValue = 0;

        int QusId = this.getResources().getIdentifier(questionPlay.get(index).getQus().toString(), "raw", getPackageName());
        btnA.setText(questionPlay.get(index).getAnswerA());
        btnB.setText(questionPlay.get(index).getAnswerB());
        btnC.setText(questionPlay.get(index).getAnswerC());
        btnD.setText(questionPlay.get(index).getAnswerD());
        mCountDown.start();
        mediaPlayer= MediaPlayer.create(this,QusId);
        mediaPlayer.start();


    } else {
        Intent intent = new Intent(this, Done.class);
        Bundle dataSend = new Bundle();
        dataSend.putInt("SCORE", score);
        dataSend.putInt("TOTAL", totalQuestion);
        dataSend.putInt("CORRECT", correctAnswer);
        intent.putExtras(dataSend);
        startActivity(intent);
        finish();
    }

}


@Override
public void onClick(View v) {

    mCountDown.cancel();
    if (index < totalQuestion) {
        Button clickedButton = (Button) v;
        if (clickedButton.getText().equals(questionPlay.get(index).getCorrectAnswer())) {

            score += 10; // increase score
            correctAnswer++; //increase correct answer
            showQuestion(++index);

        } else {
            vibrator.vibrate(50);
            showQuestion(++index); // If choose right , just go to next question
        }
        txtScore.setText(String.format("%d", score));

    }

}


//CLicking back Button
public void onBackPressed() {
    showExitAlertBox();
}

public void showExitAlertBox() {

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    alertDialog.setMessage("You want to quit the play?");

    //Yes Quit then go to category page
    alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int button) {
            Intent intent = new Intent(getApplicationContext(), CategorySecond.class);
            startActivity(intent);
        }

    });

    //no Quit stay on same page
    alertDialog.setNegativeButton("NO", null);
    alertDialog.show();
    mediaPlayer.stop();
}
}

下方显示错误

W/MediaPlayer-JNI: MediaPlayer finalized without being released

您没有正确释放媒体播放器。

使用

protected void onStop(){
    mediaPlayer.release();
    mediaPlayer = null;
}

在需要时调用此方法。

如果在 onStop 中播放,请取消计时器并停止媒体播放器

@Override
public void onStop() {
    if (mediaPlayer.isPlaying())
        mediaPlayer.stop();

    if(mCountDown != null)
       mCountDown.cancel();
    super.onStop();
}

处理销毁

@Override
public void onDestroy() {
    if (mediaPlayer.isPlaying())
        mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer = null;

    if(mCountDown != null)
       mCountDown.cancel();

}