2

I've created a simple app that has two buttons that will play and stop a song. Everything is working as it should.

However I want it to pause the song when the app goes onPause and Resume playing the song when the app is up again.

I have tried:

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    mp.start();
}

But that starts the song automatically when the app starts the first time. And it also messes up the app when you click play, the song hacks and the app crashes.

Here is the onPause and onDestroy:

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    mp.pause();
}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    mp.stop();
    mp.release();
} }
jww
  • 97,681
  • 90
  • 411
  • 885
Azad
  • 112
  • 6
  • when do you think `onResume` will be called? – Raghunandan Aug 21 '14 at 11:39
  • You have to manage this by flags only. Becuase OnResume will be called everytime when activity comes on foreground. – Hardik Trivedi Aug 21 '14 at 11:42
  • 1
    `onResume` will be called every time. Check if `mp == null` or `mp.isPlaying()` before saying `mp.start()` – Aniruddha Aug 21 '14 at 11:43
  • @Raghunandan when the app is paused and then the app is back upp again, or what do you mean? – Azad Aug 21 '14 at 11:44
  • Ive already tried `@Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); if (mp != null) { mp.start(); mp = null; } }` and mp.isPlaying() but it still starts the song when the app starts – Azad Aug 21 '14 at 11:48
  • 1
    @DroidShout after `onCreate` what will be called? onCreate -- > onStart --> onResume. read http://developer.android.com/reference/android/app/Activity.html. SO now you know why it tarts the song when the app starts. – Raghunandan Aug 21 '14 at 11:52
  • @Raghunandan so how do I prevent it to start the song? But I want it to pause the song if I hit home button and then play it again when the app get back on foreground? – Azad Aug 21 '14 at 12:07
  • @DroidShout study lifecycle of activity and media player. You will find a way yourself. read the docs will help – Raghunandan Aug 21 '14 at 12:12

2 Answers2

4

In the onResume() Method check if the song is already playing you shouldn't start it again. So now your onResume becomes

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    if (mp != null && mp.getCurrentPosition() > 0) {
        mp.start();
    }
}

This will fix your crash.

Now for the second part, how do you resume playing from the position where it was paused. As per the Documentation MediaPlayer class has two method getCurrentPosition() and seekTo(int msec) You can use SharedPreferences to store the currentPosition in onPause() Method and in your onResume() get the position for the SharedPreferences and seekTo(int msec) that position and then start playback again.

droidchef
  • 2,237
  • 1
  • 18
  • 34
2

Try the below code

@Override
protected void onResume() {
    super.onResume();
    if (mp != null && mp.getCurrentPosition() > 0) {
        mp.start();
    }
}
Mahesh
  • 974
  • 6
  • 12