android - 即使应用被杀死,声音仍会继续播放

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

我正在制作一个有声音游戏的测验应用程序。但是我发现即使关闭应用程序,声音仍在播放。我有两次使用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 finalized without being released

最佳答案

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

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

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

关于android - 即使应用被杀死,声音仍会继续播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42359698/

相关文章:

android - Android 应用程序名称消失

Android 11 - 无线 ADB 配对 - Windows 机器

java - ListView 未正确填充/不可见

Android Studio : Error inflating class android. support.design.widget.FloatingActionButton

audio - 用于打开音频文件的简单 Verilog VPI 模块

android - 在 Media Player Android 上播放来自 MediaStore 的音频

android - Android 应用程序中 CCAvenue 支付网关的沙盒模式

Android Studio 无法固定标签

java - 获取FloatBuffer的AudioInputStream

matlab - 在MATLAB中从波形音频文件中消除噪声