From the moment when the MediaPlayer instance is created to the moment it is terminated (reused or removed), the MediaPlayer object completes a series of transitions from one state to another.
The list of states is defined in MediaPlayer.PlayerState. You can retrieve the current state of the MediaPlayer object with MediaPlayer.getStatus.
PlayerState getStatus() throws IllegalStateException;
Knowing the player’s state is useful because some operations are permitted only while the player is in a particular state. For example, play cannot be called while in IDLE. It must be called after reaching the PREPARED state.
The ERROR state also changes what can happen next.
Here is how the basic procedure for loading a media resource inside the MediaPlayer corresponds to state transitions:
The initial state is IDLE.
Your application calls MediaPlayer.replaceCurrentItem, which moves the player to INITIALIZING.
The PSDK loads the resource. If successful, the state becomes INITIALIZED.
Your application calls MediaPlayer.prepareToPlay. The state becomes PREPARING.
The PSDK prepares the media stream and starts the ad resolving and ad insertion (if enabled).
When this finishes (either ads are inserted into the timeline or the ad procedure has failed), the player state becomes PREPARED.
As your application plays and pauses the media, the state moves among PLAYING and PAUSED***Android n/a***.
When the player reaches the end of the stream, the state becomes COMPLETE.
When your application releases the media player, the state becomes RELEASED.
If an error occurs at any time during the process, the state becomes ERROR.
The following state-transition diagram shows the lifecycle of a MediaPlayer instance.
You can use the state to provide feedback to the user on the process (for example, a spinner while waiting for the next state change) or to take the next steps 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(); ..... } }