Android 即使应用程序被关闭,声音仍会继续播放

Android 即使应用程序被关闭,声音仍会继续播放,android,android-studio,audio,media-player,android-mediaplayer,Android,Android Studio,Audio,Media Player,Android Mediaplayer,我正在制作一个有声音游戏的智力竞赛应用程序。但我发现即使应用程序关闭,声音也会一直播放。有两次我使用了mediaplayer 1.单击图像按钮时 2.关于变更问题 问题每15秒更改一次,在终止应用程序后,声音每15秒播放一次 imageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mediaP

我正在制作一个有声音游戏的智力竞赛应用程序。但我发现即使应用程序关闭,声音也会一直播放。有两次我使用了
mediaplayer

1.单击图像按钮时

2.关于变更问题

问题每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();
}
}
imageButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
mediaPlayer.start();
}
});
btnA.setOnClickListener(此);
btnB.setOnClickListener(此);
btnC.setOnClickListener(此);
btnD.setOnClickListener(此);
//注意:这是自动生成的,用于实现应用程序索引API。
//看https://g.co/AppIndexing/AndroidStudio 了解更多信息。
}
@凌驾
受保护的void onResume()
{
super.onResume();
questionPlay=db.getMorseQuestionMode(模式);
totalQuestion=questionPlay.size();
mCountDown=新的倒计时(超时、间隔){
@凌驾
公共void onTick(长毫秒未完成){
progressBar.setProgress(progressValue);
progressValue++;
}
@凌驾
公共无效onFinish(){
mCountDown.cancel();
showQuestion(++索引);
}
};
显示问题(索引);
}
私有void showQuestion(整数索引){
如果(索引<总问题){
这个问题++;
setText(String.format(“%d/%d”,thisQuestion,totalQuestion));
progressBar.setProgress(0);
progressValue=0;
int QusId=this.getResources().getIdentifier(questionPlay.get(index.getQus().toString(),“raw”,getPackageName());
setText(questionPlay.get(index.getAnswerA());
btnB.setText(questionPlay.get(index.getAnswerB());
setText(questionPlay.get(index.getAnswerC());
setText(questionPlay.get(index.getAnswerD());
mCountDown.start();
mediaPlayer=mediaPlayer.create(这个,QusId);
mediaPlayer.start();
}否则{
Intent Intent=新的Intent(这个,Done.class);
Bundle dataSend=新Bundle();
dataSend.putInt(“分数”,分数);
dataSend.putInt(“总计”,总计问题);
dataSend.putInt(“正确”,回答正确);
意向。额外支出(数据结束);
星触觉(意向);
完成();
}
}
@凌驾
公共void onClick(视图v){
mCountDown.cancel();
如果(索引<总问题){
按钮点击按钮=(按钮)v;
if(clickedButton.getText().equals(questionPlay.get(index.getCorrectAnswer())){
分数+=10;//增加分数
correctAnswer++;//增加正确答案
showQuestion(++索引);
}否则{
振动器。振动(50);
showQuestion(++索引);//如果选择正确,请转到下一个问题
}
setText(String.format(“%d”,score));
}
}
//单击后退按钮
public void onBackPressed(){
showExitAlertBox();
}
公开作废showExitAlertBox(){
AlertDialog.Builder AlertDialog=新建AlertDialog.Builder(此);
setMessage(“您想退出该剧吗?”);
//是退出,然后进入分类页面
alertDialog.setPositiveButton(“是”,新的DialogInterface.OnClickListener(){
@凌驾
公共void onClick(对话框接口对话框,int按钮){
Intent Intent=新的Intent(getApplicationContext(),CategorySecond.class);
星触觉(意向);
}
});
//没有退出保持在同一页上
alertDialog.setNegativeButton(“否”,null);
alertDialog.show();
mediaPlayer.stop();
}
}
下面显示我们时出错

W/MediaPlayer JNI:MediaPlayer未经发布即完成


您尚未正确释放mediaplayer

使用


如果需要,请调用此方法。

您尚未正确释放mediaplayer

使用


如果需要,请调用此方法。

如果在桌面上播放,请取消计时器并停止mediaplayer

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

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

取消计时器,如果在桌面上播放,则停止mediaplayer

@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();

}
对付毁灭

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

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

}

那么,您没有在不需要的地方调用它。您是否已保护void onResume();受保护的void onPause();顶部受保护的void();受保护的空onDestroy();在你的应用程序中?不。实际上声音的持续时间不到2秒。所以我觉得没用。因为这就像识别声音并单击答案一样。您将需要这些方法。在受保护的void onStop()中;受保护的空onDestroy();如果不需要调用MediaPlayer,请释放它。是否已保护void onResume();受保护的void onPause();顶部受保护的void();受保护的空onDestroy();在你的应用程序中?不。实际上声音的持续时间不到2秒。所以我觉得没用。因为这就像识别声音并单击答案一样。您将需要这些方法。在受保护的void onStop()中;受保护的空onDestroy();释放mediaplayerCall
mediaPlayer.reset();mediaPlayer.release();mediaPlayer=null
ondestory()方法中。调用
mediaPlayer.reset();mediaPlayer.release();mediaPlayer=null
onDestroy()中的
method.@PurshottamGusain如果有效,请接受它,这样它将对其他人有帮助。@PurshottamGusain如果有效,请接受它,这样它将对其他人有帮助。当应用程序关闭时,OnDestroy回调方法不能确定调用。当应用程序关闭时,OnDestroy回调方法不能确定调用。