Show Menu
TOPICS×

Lifecycle and statuses of the MediaPlayer object

The status of the media player determines which actions are legal.
For working with media player statuses:
  • You can retrieve the current status of the MediaPlayer object with MediaPlayer.getStatus() .
  • The list of statuses is defined in the MediaPlayerStatus enum.
Status-transition diagram for the lifecycle of a MediaPlayer instance:
The following table provides details about the lifecycle and statuses of the media player:
Status Occurs when
IDLE
The media player's initial status. The player is created and is waiting for you to specify a media player item.
INITIALIZING
Your application calls MediaPlayer.replaceCurrentItem() .
The media player item is loading.
INITIALIZED
TVSDK successfully set the media-player item.
PREPARING
Your application calls MediaPlayer.prepareToPlay() . The media player is loading the media player item and any associated resources.
PREPARED
TVSDK has prepared the media stream and has attempted to perform ad resolving and ad insertion (if enabled). The content is prepared and ads have been inserted in the timeline, or the the ad procedure failed.
Buffering or playback can begin.
PLAYING/PAUSED
As the application plays and pauses the media, the media player moves between these statuses.
SUSPENDED
If the application navigates away from the playback, shuts down the device, or switches applications while the player is playing or paused, the media player is suspended and resources are released.
Calling MediaPlayer.restore() returns the player to the status the player was in before it was SUSPENDED. The exception is if the player is SEEKING when suspended is called, the player is PAUSED and then SUSPENDED.
Important:
Remember the following information:
  • The MediaPlayer automatically calls suspend only when the surface object that is used by the MediaPlayerView is destroyed.
  • The MediaPlayer automatically calls restore() only when a new surface object that is used by the MediaPlayerView is created.
If you always want playback to be paused when the MediaPlayer is restored, have your application call MediaPlayer.pause() in the Android Activity's onPause() method.
COMPLETE
The player has reached the end of the stream, and playback has stopped.
RELEASED
Your application 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 the application can do next. For more information, see Set up error handling .
You can use the status to provide feedback on the process, or example, a spinner while waiting for the next status change, or take the next steps in playing the media, such as waiting for the appropriate status before calling the next method.
For example:
mediaPlayer.addEventListener(MediaPlayerEvent STATUS_CHANGED, new StatusChangeEventListener() { 
    @Override  
    public void onStatusChanged(MediaPlayerStatusChangeEvent event) { 
        switch(event.getStatus()) { 
            case INITIALIZED: 
                mediaPlayer.prepareToPlay(); 
                break; 
            case PREPARING: 
                showBufferingSpinner(); 
                break; 
            case PREPARED: 
                hideBufferingSpinner(); 
                mediaPlayer.play(); 
                break; 
            ...                
        } 
        ... 
    } 
});