【问题标题】:Sound keeps on playing even when app is killed即使应用程序被杀死,声音也会继续播放
【发布时间】:2017-02-21 05:54:16
【问题描述】:

我正在制作一个有声音游戏的测验应用程序。但我发现即使应用程序关闭,声音也会继续播放。我两次使用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();
}
}

在下方显示错误

W/MediaPlayer-JNI:MediaPlayer 最终确定但未发布

【问题讨论】:

  • onDestroy()方法中调用mediaPlayer.reset(); mediaPlayer.release(); mediaPlayer = null

标签: android android-studio audio media-player android-mediaplayer


【解决方案1】:

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

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

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

【讨论】:

  • @PurshottamGusain 如果它有效,请接受它,以便对其他人有所帮助。
【解决方案2】:

处理销毁

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

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

}

【讨论】:

  • 应用关闭时不会调用 OnDestroy 回调方法。
【解决方案3】:

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

使用

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

在需要时调用此方法。

【讨论】:

  • 那你不是在不需要的地方调用它
  • 你是否保护过 void onResume();受保护的无效 onPause();受保护的无效 onStop();受保护的无效 onDestroy();在您的应用中?
  • 没有。实际上声音的持续时间不到2秒。所以没觉得有用。因为这就像识别声音并点击答案。
  • 您将需要这些方法。在受保护的 void onStop();受保护的无效 onDestroy();释放媒体播放器
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多