android音视频播放学习之MediaPlayer学习记录
最近项目中涉及到音视频开发,选择MediaPlayer+surfaceView的方式播放视频。翻阅开发者文档学习了解下MediaPlaer的介绍和使用,在这里记录下对MediaPlaer的理解。
MediaPlayer可以用于播放本地、网络或者apk包中的音视频文件,用setDataSource()方法设置文件的路径。首先MediaPlayer是一个状态机,有下面几个状态:idle(闲置)、initialized、prepared、start、pause、stop、playbackCompleted、end、error。当mediaplayer使用new创建出来或者调用了reset()方法后mediaplayer处于idle状态,此时需要调用prepare()(同步方法)或者
prepareAsync()(异步方法)才能使mediaplayer处于可用状态。
当mediaplayer刚创建或调用reset()方法使mediaplayer处于idle状态调用start()等方法时出现的一些区别:
当mediaplayer刚创建时,调用start()方法,mediaplayer的状态不会改变,如果设置了OnErrorListener,其中的onError()方法不会被调用。
当调用reset()方法处于idle状态时调用start()方法会是mediaplayer处于error状态,此时
如果设置了OnErrorListener,其中的onError()方法将会被调用。
调用 release()会释放的资源,使mediaplayer处于end状态,此时在调用其他方法都不会改变mediaplayer的状态,此方法调用应该在不再使用mediaplayer时调用。
MediaPlayer可以设置一些监听:
-
setOnPreparedListener(android.media.MediaPlayer.OnPreparedListener)
. -
setOnSeekCompleteListener(OnSeekCompleteListener)
. -
setOnCompletionListener(OnCompletionListener)
. setOnErrorListener(android.media.MediaPlayer.OnErrorListener)
-
setOnBufferingUpdateListener(OnBufferingUpdateListener)
.
mediaplayer中的方法需要在对应的状态中调用否则会将mediaplayer转为error状态,以下为各个方法调用的有效状态和无效状态表格:
-
Valid Sates Invalid States Comments getCurrentPosition {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} {Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. getDuration {Prepared, Started, Paused, Stopped, PlaybackCompleted} {Idle, Initialized, Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. getVideoHeight {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} {Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. getVideoWidth {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} {Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. isPlaying {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} {Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. pause {Started, Paused} {Idle, Initialized, Prepared, Stopped, PlaybackCompleted, Error} Successful invoke of this method in a valid state transfers the object to the Paused state. Calling this method in an invalid state transfers the object to the Error state. prepare {Initialized, Stopped} {Idle, Prepared, Started, Paused, PlaybackCompleted, Error} Successful invoke of this method in a valid state transfers the object to the Prepared state. Calling this method in an invalid state throws an IllegalStateException. prepareAsync {Initialized, Stopped} {Idle, Prepared, Started, Paused, PlaybackCompleted, Error} Successful invoke of this method in a valid state transfers the object to the Preparing state. Calling this method in an invalid state throws an IllegalStateException. release any {} After release()
, the object is no longer available.reset {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error} {} After reset()
, the object is like being just created.seekTo {Prepared, Started, Paused, PlaybackCompleted} {Idle, Initialized, Stopped, Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. setAudioStreamType {Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted} {Error} Successful invoke of this method does not change the state. setDataSource {Idle} {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error} Successful invoke of this method in a valid state transfers the object to the Initialized state. Calling this method in an invalid state throws an IllegalStateException. setDisplay any {} This method can be called in any state and calling it does not change the object state. setLooping {Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted} {Error} Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. isLooping any {} This method can be called in any state and calling it does not change the object state. setOnBufferingUpdateListener any {} This method can be called in any state and calling it does not change the object state. setOnCompletionListener any {} This method can be called in any state and calling it does not change the object state. setOnErrorListener any {} This method can be called in any state and calling it does not change the object state. setOnPreparedListener any {} This method can be called in any state and calling it does not change the object state. setOnSeekCompleteListener any {} This method can be called in any state and calling it does not change the object state. setScreenOnWhilePlaying any {} This method can be called in any state and calling it does not change the object state. setVolume {Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted} {Error} Successful invoke of this method does not change the state. setWakeMode any {} This method can be called in any state and calling it does not change the object state. start {Prepared, Started, Paused, PlaybackCompleted} {Idle, Initialized, Stopped, Error} Successful invoke of this method in a valid state transfers the object to the Started state. Calling this method in an invalid state transfers the object to the Error state. stop {Prepared, Started, Stopped, Paused, PlaybackCompleted} {Idle, Initialized, Error} Successful invoke of this method in a valid state transfers the object to the Stopped state. Calling this method in an invalid state transfers the object to the Error state.