I have the following code snippet. Not sure why, when clicked on the exit button the media player doesn't stop, even though exiting the game should stop all activities in it. Any help would be appreciated.I have tried stop(), release(), reset() and setting to null. Please let me know where am I going wrong.

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

        }
有帮助吗?

解决方案

Please try the following :

create your media player on the on create and declare it as a member of your ACTIVITY :

MediaPlayer back_music;
public class MainACtivity(){
....
....
onCreate(){
   back_music= MediaPlayer.create(getBaseContext(), R.raw.sher_khan);
.....
}

then delete the the row "MediaPlayer back_music = MediaPlayer.create(getBaseContext(), R.raw.sher_khan);"

and give it a another shot :)

hope it helps you .

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top