0

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

        }
artsylar
  • 2,648
  • 4
  • 34
  • 51
suman das
  • 5
  • 4

1 Answers1

0

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 .

dbkoren
  • 1,305
  • 1
  • 11
  • 16
  • Hi dbkoren, Thanks a lot, yes it did help and now easier for me to understand the structure a little bit more. – suman das Aug 18 '13 at 15:08
  • Try to work with an example from the Web when working on new stuff it all ways helps... Please accept the answer if you find it working and useful. Thank you. – dbkoren Aug 18 '13 at 15:19
  • Actually I missed the point why it should be declared in onCreate(), that is the primary activity of the application which should decide if to keep the resources holding or not. Thanks again for the guidance! – suman das Aug 18 '13 at 15:22