MediaPlayer.SeekTo(int) throws IllegalStateException
-
12-12-2019 - |
Question
Sometimes I get IllegalStateException thrown by MediaPlayer.SeekTo(int) The MediaPlayer runs in a Service, and a call the SeekTo(int) like this:
public void seekTo(int value)
{
if (mp != null && !(value <= -1)) {
mp.seekTo(value);
}
}
Solution
IllegalStateException
is thrown to prevent programming errors such as calling prepare()
, prepareAsync()
, or one of the overloaded setDataSource
methods in an invalid state.
An IllegalStateException
is thrown if prepare()
or prepareAsync()
is called in any other state.
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.
This data is all covered here: http://developer.android.com/reference/android/media/MediaPlayer.html In fact I directly quoted it.
You should be able to handle any errors encountered using an OnErrorListener()
.
You can find more about an OnErrorListener()
here:
http://developer.android.com/reference/android/media/MediaPlayer.OnErrorListener.html
In the error listener you will:
- Receive the error.
- Determine the type of error.
- Handle the error to rectify the situation without crashing.