2012年3月11日 星期日

[AndroidMultimedia] Mediaplayer APIs

當在執行 MediaPlayer 中有錯,則會呼叫 OnErrorListener.onError()。


Prepared state
  • Preparing state is a transient state.
  • A MediaPlayer object must first enter the Prepared state before playback can be started.
  • When the preparation completes or when prepare() call returns, the internal player engine then calls a user supplied callback method, onPrepared() of the OnPreparedListener interface (setOnPreparedListener(android.media.MediaPlayer.OnPreparedListener)).
  • While in the Prepared state, properties such as audio/sound volume, screenOnWhilePlaying, looping can be adjusted by invoking the corresponding set methods.
  • The call to prepare() can take a long time to execute, because it might involve fetching and decoding media data. So, you should never call it from your application's UI thread. 
    • To avoid hanging your UI thread, spawn another thread to prepare the MediaPlayer and notify the main thread when done. 
    • Convenient way to accomplish this task by using the prepareAsync() method. 


Start state
  • After start() returns successfully, the MediaPlayer object is in the Started state. isPlaying() can be called to test whether the MediaPlayer object is in the Started state.
  • While in the Started state, the internal player engine calls a user supplied OnBufferingUpdateListener.onBufferingUpdate()
    • This callback allows applications to keep track of the buffering status while streaming audio/video.
  • Note that the transition from the Started state to the Paused state and vice versa happens asynchronously in the player engine.


Stop state
  • Once in the Stopped state, playback cannot be started until prepare() or prepareAsync() are called to set the MediaPlayer object to the Prepared state again.


Play position


When the playback reaches the end of stream, the playback completes.
  • The looping mode(setLooping(boolean)):
    • True: the MediaPlayer object shall remain in the Started state.
    • False, the player engine calls a user supplied callback method, OnCompletion.onCompletion(), (setOnCompletionListener(OnCompletionListener)) The invoke of the callback signals that the object is now in the PlaybackCompleted state.
  • While in the PlaybackCompleted state, calling start() can restart the playback from the beginning of the audio/video source.


Permission
  • <uses-permission android:name="android.permission.INTERNET" />
  • <uses-permission android:name="android.permission.WAKE_LOCK" />


States
  • Create successful -> Idle -> setDataResource() -> Initialized -> prepare() or prepareAsync() -> Prepared -> start()/pause()/seekTo() -> Started/Paused/PlaybackCompleted
  • Error -> reset() -> Idle
  • Calling start()/pause()/stop() has not effect on a MediaPlayer object that is already in the Started/Paused/Stopped state.



* Reference
- MediaPlayer API
MediaPlayer State Diagram **
- MediaPlayer Guide
- media-formats
-  利用Android的MediaPlayer播放影片並動態調整影片畫面大小

沒有留言:

張貼留言