From the moment that you create the MediaPlayer instance to the moment when you terminate (reuse or remove) it, this instance completes a series of transitions between states.
Some operations are permitted only when the player is in a particular state. For example, calling play
in IDLE
is not allowed. You can call this status only after the player reaches the PREPARED
state.
To work with states:
You can retrieve the current state of the MediaPlayer
object with MediaPlayer.getStatus
.
PlayerState getStatus() throws IllegalStateException;
The list of states is defined in MediaPlayer.PlayerState
.
State-transition diagram for the lifecycle of a MediaPlayer
instance:
The following table provides additional details:
MediaPlayer.PlayerState | Occurs when |
---|---|
IDLE | Your application requested a new media player by calling DefaultMediaPlayer.create . The newly created player is waiting for you to specify a media player item. This is the media player's initial state. |
INITIALIZING | Your application called MediaPlayer.replaceCurrentItem , and the media player is loading. |
INITIALIZED | TVSDK successfully set the media player item. |
PREPARING | Your application called MediaPlayer.prepareToPlay . The media player is loading the media player item and the associated resources. Tip: Some buffering of the main media might occur. TVSDK is preparing the media stream and attempting to perform ad resolving and ad insertion, (if enabled). Tip: To set the start time to a non-zero value, call prepareToPlay(startTime) with the time in milliseconds. |
PREPARED | The content is prepared and ads have been inserted in the timeline, or the ad procedure failed. Buffering or playback can begin. |
PLAYING | Your application has called play , so TVSDK is trying to play the video. Some buffering might occur before the video actually plays. |
PAUSED | As your application plays and pauses the media, the media player moves between this state and PLAYING. |
SUSPENDED | Your application navigated away from the playback, shut down the device, or switched applications while the player was playing or paused. The media player has been suspended and resources have been released. To continue, restore the media player. |
COMPLETE | The player reached the end of the stream, and playback has stopped. |
RELEASED | Your application has released the media player, which also releases any associated resources. You can no longer use this instance |
ERROR | An error occurred during the process. An error also might affect what your application can do next. |
You can use the state to provide feedback on the process (for example, a spinner while waiting for the next state change) or to take the next step in playing the media, such as waiting for the appropriate state before calling the next method.
For example:
@Override
public void onStateChanged(MediaPlayer.PlayerState state,
MediaPlayerNotification notification) {
switch (state) {
// It is recommended that you call prepareToPlay() after receiving
// the INITIALIZED state.
case INITIALIZED:
_mediaPlayer.prepareToPlay();
break;
case PREPARING:
showBufferingSpinner();
break;
case PREPARED:
hideBufferingSpinner();
.....
}
}