Update MediaPlayer Start Position When Paused

Brianjs picture Brianjs · Mar 26, 2012 · Viewed 7.8k times · Source

I am streaming live audio and wish to have the functionality to pause the mediaplayer and then start from the current live position. The default Pause() and Start() just starts from where it was stopped. Any ideas on how to keep the current position updated so that Start() is instant?

Answer

bughi picture bughi · Mar 26, 2012

Did you try calling seekTo()?

You could keep it pemanently updated by calling seekTo() every half a second or so with TimerTask, But i don't know how i feel about this.

EDIT: If you call seekTo() every 500 milliseconds, in order to keep it moving forward you would call it with the currentPosition + time ellapsed since last call(500 milliseconds in this case). But again I don't know if this is the best aproach. Another way is to create a OnSeekCompleteListener that preforms a new seekTo(currentPosition + timeEllapsed). In this case you need to calculate timeEllapsed like so: currentSystemTime - systemTimeOnLastCall

From http://developer.android.com/reference/android/media/MediaPlayer.html:

The playback position can be adjusted with a call to seekTo(int).

Although the asynchronuous seekTo(int) call returns right way, the actual seek operation may take a while to finish, especially for audio/video being streamed. When the actual seek operation completes, the internal player engine calls a user supplied OnSeekComplete.onSeekComplete() if an OnSeekCompleteListener has been registered beforehand via setOnSeekCompleteListener(OnSeekCompleteListener).

Please note that seekTo(int) can also be called in the other states, such as Prepared, Paused and PlaybackCompleted state.

Furthermore, the actual current playback position can be retrieved with a call to getCurrentPosition(), which is helpful for applications such as a Music player that need to keep track of the playback progress.