1

I am working on a music player and everything is fine the play, stop, and pause buttons. My problem is that whenever a song is playing and I press the back button and reopen the app the buttons won't work with the current song.

This happens as well when I change the orientation. Is there a way I can save the state of the media player and then obtain the state so that I can stop the song that is being played?

Jonik
  • 80,077
  • 70
  • 264
  • 372
Javier Pech
  • 29
  • 1
  • 4
  • you can save the where the song stopped , using preferences or Bundle to save the state and restart the media player again from where it stopped take look on this thread http://stackoverflow.com/questions/17917994/how-to-play-audio-continuously-while-orientation-in-android – mohammed momn Jan 27 '14 at 22:10
  • thanks for the orientation part but now my question would be how to work around when the app quits because whenever I reopen the app the stop button wont work with the current song. thanks for the help guys... – Javier Pech Jan 27 '14 at 22:23
  • when you quite from app using shared Preferences to save the state – mohammed momn Jan 27 '14 at 22:24
  • ok thank a lot am gonna work on that. – Javier Pech Jan 27 '14 at 22:29

2 Answers2

0

Well, you could use

.getCurrentPosition() 

to get how many milliseconds into thew song you are, then use

.seekTo(int milliseconds)

upon resuming or an orientation change, to get to the same spot.

ArmaAK
  • 587
  • 6
  • 21
0

You can use the SeekTo method. Seek To

Then add:

protected void onSaveInstanceState(Bundle out) {
    super.onSaveInstanceState(out);
    out.putInt(PLAY_TIME, mMediaPlayer.getCurrentPosition());
}

And later in the OnCreate, you just need to check something like this:

if(savedInstanceState != null && savedInstanceState.containsKey(PLAY_TIME)) {
        mMediaPlayer.seekTo(savedInstanceState.getInt(PLAY_TIME, 0));
    }

Hope this helps

hmartinezd
  • 1,188
  • 8
  • 11