1

I was reading many tips and answers to similar questions but can't get it done with my piece of code. The isse is that music is played in background even when you exit the app. Can you help me to fix this? I have the button which clicked starts the music and clicked the second time pause it. This works what I want to achieve is to stop music play, when the user exits the app eather by clicking home or back button.

    ImageButton playmusic;

    playmusic = (ImageButton)this.findViewById(R.id.listen_button);
    final MediaPlayer mp = MediaPlayer.create(this, R.raw.music);
    playmusic.setOnClickListener(new OnClickListener(){

        public void onClick(View v) {

              if(mp.isPlaying() == true)
                      mp.pause();

            else

                mp.start();

        }
      });
    }
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
Pawel
  • 57
  • 11

1 Answers1

0

Try Overriding the onPause method

@Override
public void onPause() {
    super.onPause();
    //Pause your player
    mp.pause();
    //do more stuff
}

When opening again if you want to start playing again Override onResume:

@Override
public void onResume() {
    super.onResume();
    //Play again
    mp.play();
    //do more stuff
}

Hope it helps

DarkCygnus
  • 7,420
  • 4
  • 36
  • 59
  • Thanks a lot. But should your code exchange my code or go somewhere else (end of the MainActivity code? I find it difficult to catch the place not to have errors in other parts of my code (I have the listView in this activity). Thanks in advance – Pawel Jun 11 '16 at 05:35
  • You should add it to your activity, aditional to the methods you already have. That is, the activity itself overrides those functions. If you type Ctrl+O you can see all the methods you can Override. – DarkCygnus Jun 11 '16 at 05:38
  • Yes at the end is ok to add it, so you dont have to scroll over it every time – DarkCygnus Jun 11 '16 at 05:39
  • Just make sure you can access all variables like `mp` (maybe declare `mp` globally?) – DarkCygnus Jun 11 '16 at 05:42
  • 1
    Thanks for comments, keep trying - it is not easy when you start learing ;) – Pawel Jun 11 '16 at 05:45
  • If it were easy it would not be fun :) update me when you try it so I can follow up. Cheers – DarkCygnus Jun 11 '16 at 05:53
  • I think I am closer and closer - thank you for your support. And yeah - I do agree with your opinion and that feeling when code works! I placed the code and the last isssue I have is The method play() is undefined for the type MediaPlayer with onResume mp.play(); - any advice? – Pawel Jun 11 '16 at 06:11
  • I have declared mp globally MediaPlayer mp; and added it at the end of code but it makes my app crashes when any item on listview is clicked or home button is clicked @Override public void onPause() { super.onPause(); mp.pause(); } – Pawel Jun 11 '16 at 06:49
  • Try just calling `super.onPause` and `super.onResume` on your overriden methods. (That is comment out the `mp` lines). I believe it should also work that way. You may have to return `mp` to the way it was declared before. (Sorry took some time to respond, it was already late here in my country) – DarkCygnus Jun 11 '16 at 14:21
  • What does the crash log say – DarkCygnus Jun 11 '16 at 14:26
  • Thanks GrayCygnus for reply and your time. I was trying to put those everywhere but always got errors: once when clicking item on listview, other time when button or home is clicked and otherwise just during app start . I believe it is the place/way I enter this part which makes this and this is why it is a little bit frustrating. I also found nice, other solution but it doesn't work eather. I tried comment out mp lines but no effect. What I get most of the time: "Unable to pause activity (...); java lang.NullPointerException – Pawel Jun 11 '16 at 15:50
  • Check [this](http://stackoverflow.com/questions/15327050/mediaplayer-pausing-and-playing-error) question. I believe its the way you instantiate your `mp`. You are passing `this` to the constructor, but maybe the scope of `this` is not the correct one. Try passing `nameofyourclass.this` to the player instead. – DarkCygnus Jun 11 '16 at 16:22