我有以下代码片段。不知道为什么,当单击退出按钮时,媒体播放器不会停止,即使退出游戏应该会停止其中的所有活动。任何帮助将不胜感激。我尝试过 stop()、release()、reset() 并设置为 null。请让我知道我哪里错了。
public void onClick(View v){
// The background music of the game
MediaPlayer back_music = MediaPlayer.create(getBaseContext(), R.raw.sher_khan);
switch (v.getId()){
case R.id.new_game:
openNewGameDialog();
break;
case R.id.about_game :
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.exit_game :
if(back_music.isPlaying()){
back_music.release();
back_music.reset();
back_music = null ;
//onDestroy();
}
finish();
break;
case R.id.sound :
// Looping the music
//back_music.setLooping(true);
// Identifying and kind of looping through the sound_selector items
if(v.isSelected()){
v.setSelected(false);
play = false;
//Music start for the media player
back_music.start();
}
else if (!v.isSelected()){
//speaker.setSelected(false);
back_music.stop();
back_music.release();
v.setSelected(true);
play = true;
back_music.release();
}
}