1. 状态机:



转自:http://developer.android.com/reference/android/media/MediaPlayer.html


State Diagram

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 MediaPlayer object driven by the supported playback control operations. The ovals represent the states a MediaPlayer object may reside in. The arcs represent the playback control operations that drive the object state transition. There are two types of arcs. The arcs with a single arrow head represent synchronous method calls, while those with a double arrow head represent asynchronous method calls.

From this state diagram, one can see that a MediaPlayer object has the following states:

  • When a MediaPlayer object is just created usingnewor afterreset()is called, it is in theIdlestate; and afterrelease()is called, it is in theEndstate. Between these two states is the life cycle of the MediaPlayer object.
    • There is a subtle but important difference between a newly constructed MediaPlayer object and the MediaPlayer object afterreset()is called. It is a programming error to invoke methods such asgetCurrentPosition(),getDuration(),getVideoHeight(),getVideoWidth(),setAudioStreamType(int),setLooping(boolean),setVolume(float, float),pause(),start(),stop(),seekTo(int),prepare()orprepareAsync()in theIdlestate for both cases. If any of these methods is called right after a MediaPlayer object is constructed, the user supplied callback method OnErrorListener.onError() won't be called by the internal player engine and the object state remains unchanged; but if these methods are called right afterreset(), the user supplied callback method OnErrorListener.onError() will be invoked by the internal player engine and the object will be transfered to theErrorstate.
    • It is also recommended that once a MediaPlayer object is no longer being used, callrelease()immediately so that resources used by the internal player engine associated with the MediaPlayer object can be released immediately. Resource may include singleton resources such as hardware acceleration components and failure to callrelease()may cause subsequent instances of MediaPlayer objects to fallback to software implementations or fail altogether. Once the MediaPlayer object is in theEndstate, it can no longer be used and there is no way to bring it back to any other state.
    • Furthermore, the MediaPlayer objects created usingnewis in theIdlestate, while those created with one of the overloaded convenientcreatemethods areNOTin theIdlestate. In fact, the objects are in thePreparedstate if the creation usingcreatemethod is successful.
  • In general, some playback control operation may fail due to various reasons, such as unsupported audio/video format, poorly interleaved audio/video, resolution too high, streaming timeout, and the like. Thus, error reporting and recovery is an important concern under these circumstances. Sometimes, due to programming errors, invoking a playback control operation in an invalid state may also occur. Under all these error conditions, the internal player engine invokes a user supplied OnErrorListener.onError() method if an OnErrorListener has been registered beforehand viasetOnErrorListener(android.media.MediaPlayer.OnErrorListener).
    • It is important to note that once an error occurs, the MediaPlayer object enters theErrorstate (except as noted above), even if an error listener has not been registered by the application.
    • In order to reuse a MediaPlayer object that is in theErrorstate and recover from the error,reset()can be called to restore the object to itsIdlestate.
    • It is good programming practice to have your application register a OnErrorListener to look out for error notifications from the internal player engine.
    • IllegalStateException is thrown to prevent programming errors such as callingprepare(),prepareAsync(), or one of the overloadedsetDataSourcemethods in an invalid state.
  • CallingsetDataSource(FileDescriptor), orsetDataSource(String), orsetDataSource(Context, Uri), orsetDataSource(FileDescriptor, long, long)transfers a MediaPlayer object in theIdlestate to theInitializedstate.
    • An IllegalStateException is thrown if setDataSource() is called in any other state.
    • It is good programming practice to always look out forIllegalArgumentExceptionandIOExceptionthat may be thrown from the overloadedsetDataSourcemethods.
  • A MediaPlayer object must first enter thePreparedstate before playback can be started.
    • There are two ways (synchronous vs. asynchronous) that thePreparedstate can be reached: either a call toprepare()(synchronous) which transfers the object to thePreparedstate once the method call returns, or a call toprepareAsync()(asynchronous) which first transfers the object to thePreparingstate after the call returns (which occurs almost right way) while the internal player engine continues working on the rest of preparation work until the preparation work completes. When the preparation completes or whenprepare()call returns, the internal player engine then calls a user supplied callback method, onPrepared() of the OnPreparedListener interface, if an OnPreparedListener is registered beforehand viasetOnPreparedListener(android.media.MediaPlayer.OnPreparedListener).
    • It is important to note that thePreparingstate is a transient state, and the behavior of calling any method with side effect while a MediaPlayer object is in thePreparingstate is undefined.
    • An IllegalStateException is thrown ifprepare()orprepareAsync()is called in any other state.
    • While in thePreparedstate, properties such as audio/sound volume, screenOnWhilePlaying, looping can be adjusted by invoking the corresponding set methods.
  • To start the playback,start()must be called. Afterstart()returns successfully, the MediaPlayer object is in theStartedstate.isPlaying()can be called to test whether the MediaPlayer object is in theStartedstate.
    • While in theStartedstate, the internal player engine calls a user supplied OnBufferingUpdateListener.onBufferingUpdate() callback method if a OnBufferingUpdateListener has been registered beforehand viasetOnBufferingUpdateListener(OnBufferingUpdateListener). This callback allows applications to keep track of the buffering status while streaming audio/video.
    • Callingstart()has not effect on a MediaPlayer object that is already in theStartedstate.
  • Playback can be paused and stopped, and the current playback position can be adjusted. Playback can be paused viapause(). When the call topause()returns, the MediaPlayer object enters thePausedstate. Note that the transition from theStartedstate to thePausedstate and vice versa happens asynchronously in the player engine. It may take some time before the state is updated in calls toisPlaying(), and it can be a number of seconds in the case of streamed content.
    • Callingstart()to resume playback for a paused MediaPlayer object, and the resumed playback position is the same as where it was paused. When the call tostart()returns, the paused MediaPlayer object goes back to theStartedstate.
    • Callingpause()has no effect on a MediaPlayer object that is already in thePausedstate.
  • Callingstop()stops playback and causes a MediaPlayer in theStarted,Paused,PreparedorPlaybackCompletedstate to enter theStoppedstate.
    • Once in theStoppedstate, playback cannot be started untilprepare()orprepareAsync()are called to set the MediaPlayer object to thePreparedstate again.
    • Callingstop()has no effect on a MediaPlayer object that is already in theStoppedstate.
  • The playback position can be adjusted with a call toseekTo(int).
    • Although the asynchronuousseekTo(int)call returns right way, the actual seek operation may take a while to finish, especially for audio/video being streamed. When the actual seek operation completes, the internal player engine calls a user supplied OnSeekComplete.onSeekComplete() if an OnSeekCompleteListener has been registered beforehand viasetOnSeekCompleteListener(OnSeekCompleteListener).
    • Please note thatseekTo(int)can also be called in the other states, such asPrepared,PausedandPlaybackCompletedstate.
    • Furthermore, the actual current playback position can be retrieved with a call togetCurrentPosition(), which is helpful for applications such as a Music player that need to keep track of the playback progress.
  • When the playback reaches the end of stream, the playback completes.
    • If the looping mode was being set totruewithsetLooping(boolean), the MediaPlayer object shall remain in theStartedstate.
    • If the looping mode was set tofalse, the player engine calls a user supplied callback method, OnCompletion.onCompletion(), if a OnCompletionListener is registered beforehand viasetOnCompletionListener(OnCompletionListener). The invoke of the callback signals that the object is now in thePlaybackCompletedstate.
    • While in thePlaybackCompletedstate, callingstart()can restart the playback from the beginning of the audio/video source.

更多相关文章

  1. Android 如何调用MediaPlayer播放视频[状态机]【图】
  2. Android 蓝牙状态机以及蓝牙启动状态机
  3. Android状态机
  4. android wifi状态机原理
  5. 各Android版本WifiStateMachine状态机
  6. 状态机在移动端项目中的使用
  7. 状态机设计
  8. 实现一个简单的 JavaScript 状态机[每日前端夜话0xBF]

随机推荐

  1. android修改当前窗口的亮度
  2. 使用Maven开发Android
  3. android ScrollView嵌套ListView,并且实现
  4. Android运行时权限策略和easyPermission
  5. Android中级教程之(二)----->Android(安卓)
  6. Android 使用MediaPlayer播放视频切换后
  7. Android SurfaceView 实现实时显示摄像头
  8. Android合并两张bitmap为一张
  9. java.lang.RuntimeException: Unable to
  10. Android(安卓)ndk开发swig编译jni接口配