Android Multimedia: Media Player States





  • Playback control of audio/video files and streams is managed as a state machine. 
  • The following diagram shows the life cycle and the states of a Media Player object driven by the supported playback control operations. 
  • Media Player has several internal states and can be changed by different operations. 
  • Media Player is a controller class that has a lot of low level function calls which directly invoke the functions in the native media framework library; it can handle not only video but audio content as well;
Player States:
  • Idle: When the Media Player object is created using new or after reset () is called, it is in idle state. Call the Media Player constructor to create the object; the Media Player object is in idle state when created; 
                         a) MediaPlayer mp = new MediaPlayer (); 
                              It creates new MediaPlayer object, and we can interact with the player object. 
                             After we need to initialize the player with SetDataSource (...) method. 
                         b) public static MediaPlayer create (Context context, Uri uri): 
                              It creates a MediaPlayer for a given Uri. On success the prepare () also called 
                              no need to call prepare again. 
                              After successful of create we can give command play ().                                
                              Here the argument uri is the Uri from which to get the datasource. 
                         c) public static MediaPlayer create (Context context, int resid) 
                             It create a MediaPlayer for a given resource id, on success prepare () also 
                             called, no need to call prepare again. After successful of create we can give 
                             command play (). 
                             Here the resid is the resource to use as the datasource                         
                         d) public static MediaPlayer create (Context context, Uri uri, SurfaceHolder 
                             holder) 
                             It create a MediaPlayer for a given Uri, and uses the holder to render the 
                             video, onsuccess it calls the prepare() method. After successful of create we 
                             can give command play (). 
                    Note: After b,c,d calls the player in “Prepared State”, we can give play command 
                              after successful completion of these methods. 


  • End: After release () is called it is in End state.                                                                         After calling release on MediaPlayer it will be in End state. After release, the object is no longer available. We can call this (release) method in any state of the player. 
  • Initialized: calling setDatsource () method transfers a Media player objects in the idle state to initialized state. 
          Media Player class has several setDataSource () signatures to accept different content 
          type than URI. After this call, the Media Player object is in “Initialized” state; 

          After SetDataSource we can prepare the player in two ways 
                   1) Prepare(): If it is a local file or raw file with the project resource then we can give 
                        prepare command. We can call prepare on player if player in initialized or 
                        Stopped state only. 
                   2) PrepareAsync (): If it is a URL and we need to stream the data from network 
                        and play that data in that case we need to give prepareAsync () command for non 
                        blocking functionality. We can call prepare on player if player in initialized or 
                        Stopped state only. 


  • Preparing: If we call the prepareAsync (), now the player is in preparing state. After the required buffer gathered with it will go to prepared state and it will call the onPrepared listener attached to the player object. And the player currently in Prepared State. 
  • Prepared: If we call prepare (), player is in Prepared state. 
           Media Player object must first enter the Prepared state before playback can be started. 
           In this state, the Media Player object can be operated to play, pause and stop, etc. 

  • Playing: The playback starts after start () of the Media Player object is invoked inside of the onPrepared () callback function. The Media Player object is at the “Started” state now and can be paused and stopped. 
  • Stop: Call stop () of Media Player to stop the playback. This sets the object to the “Stopped” state. Once in the “Stopped” state, the Media Player must be prepared again in order to play. 
          We can give stop command to the player if the player current state is Prepared, Started, 
          Stopped, Paused, and Playback Completed. 
  • Paused: We can give the pause command on the player if its current state is Started or Paused. 
  • Playback Complete: If the Player completes the playing of the given file, the player current state is PlaybackCompleted. In this state we can restart the player using start () command. 
  • Error: If any errors occurred while preparing or playing the player state will be in Error state. It will call the OnError listener attached to the player.

2 comments:

  1. Thanks for providing such a great article, it was excellent and very informative.
    as a first time visitor to your blog I am very impressed.
    thank you :)

    ReplyDelete
  2. Hi.. I found your articles very useful..
    Can you please complete the android framework articles soon.. or please let me know if you have resources from which i can learn android framework.

    Thanks!

    ReplyDelete