How can I save media player state?

假装没事ソ 提交于 2019-12-08 08:19:51

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/21392676/how-can-i-save-media-player-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!