Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update MediaPlayer Start Position When Paused

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?

like image 800
Brianjs Avatar asked Mar 26 '12 01:03

Brianjs


People also ask

How do I pause and resume MediaPlayer on Android?

Calling start() to resume playback for a paused MediaPlayer object, and the resumed playback position is the same as where it was paused. When the call to start() returns, the paused MediaPlayer object goes back to the Started state.

How do I know if MediaPlayer is paused?

There is no API to check if MediaPlayer is paused or not. So please use any Boolean variable to check and toggle it when you paused using any button .

What is MediaPlayer in Android explain transitions through the state machine?

Android is providing MediaPlayer class to access built-in mediaplayer services like playing audio,video e.t.c. In order to use MediaPlayer, we have to call a static Method create() of this class. This method returns an instance of MediaPlayer class. Its syntax is as follows − MediaPlayer mediaPlayer = MediaPlayer.

How do I restart MediaPlayer?

Just use pause to stop, followed by seekTo(0) before restarting: mediaPlayer.


1 Answers

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.

like image 73
bughi Avatar answered Nov 15 '22 16:11

bughi