Android MediaPlayer
来源:互联网 发布:av天堂最新域名多少 编辑:程序博客网 时间:2024/06/07 15:58
1. MediaPlayer播放流程
下面是在Java中调用MediaPlayer实现播放的简单流程
mMediaPlayer.setDataSource(filePath);mMediaPlayer.prepare();mDuration.setText(String.valueOf(mMediaPlayer.getDuration()));mVideoWidht = mMediaPlayer.getVideoWidth();mVideoHeight = mMediaPlayer.getVideoHeight();if( mVideoWidht!=0 && mVideoHeight != 0 ){mMediaPlayer.start();}
2. MediaPlayer状态转移图
椭圆:状态
箭头:操作
单箭头-->同步操作
双箭头-->>异步操作
2.1 IdleState 和 EndState
IdleState : new 或者 reset()之后
EndState:release()之后
在IdleState 和 EndState状态中不能调用getCurrentPosition()
, getDuration()
, getVideoHeight()
, getVideoWidth()
, setAudioStreamType(int)
, setLooping(boolean)
, setVolume(float, float)
, pause()
, start()
, stop()
,seekTo(int)
, prepare()
or prepareAsync()
二者的区别在于调用后的反馈不同:
IdleState: 调用OnErrorListener.onError() ,状态不变
EndState:调用OnErrorListener.onError(),状态变为ErrorState
2.2 stop
在调用stop后必须调用prepare() or prepareAsync() 才能进入started状态
2.3 prepare() prepareAsync()
作用:预处理播放器为播放做准备
二者的不同在于
prepare:同步操作
prepareAsync:异步操作, 常用用于数据源为流; prepareAsync给出的错误信息也更多
2.4 Completed State
播放结束后的状态, 其后续的状态转移可由setLooping(boolean)设置:
setLooping( true ) : 直接转移到Started状态循环播放
setLooping( false ): 播放完成后触发setOnCompletionListener(OnCompletionListener),
可调用start()重新播放
注意:此时不能调用stop操作, 否则还要进行perpare操作
3. Valid and invalid states
Method NameValid SatesInvalid StatesCommentsattachAuxEffect{Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}{Idle, Error}This method must be called after setDataSource. Calling it does not change the object state.getAudioSessionIdany{}This method can be called in any state and calling it does not change the object state.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 theError 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 theError 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 theError 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 theError 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 theError state.pause{Started, Paused, PlaybackCompleted}{Idle, Initialized, Prepared, Stopped, Error}Successful invoke of this method in a valid state transfers the object to thePaused 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 thePrepared 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 thePreparing state. Calling this method in an invalid state throws an IllegalStateException.releaseany{}Afterrelease()
, 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 theError state.setAudioSessionId{Idle}{Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error}This method must be called in idle state as the audio session ID must be known before calling setDataSource. Calling it does not change the object state.setAudioStreamType{Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted}{Error}Successful invoke of this method does not change the state. In order for the target audio stream type to become effective, this method must be called before prepare() or prepareAsync().setAuxEffectSendLevelany{}Calling this method does not change the object state.setDataSource{Idle}{Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error}Successful invoke of this method in a valid state transfers the object to theInitialized state. Calling this method in an invalid state throws an IllegalStateException.setDisplayany{}This method can be called in any state and calling it does not change the object state.setSurfaceany{}This method can be called in any state and calling it does not change the object state.setVideoScalingMode{Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}{Idle, Error}Successful invoke of this method does not change the 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 theError state.isLoopingany{}This method can be called in any state and calling it does not change the object state.setOnBufferingUpdateListenerany{}This method can be called in any state and calling it does not change the object state.setOnCompletionListenerany{}This method can be called in any state and calling it does not change the object state.setOnErrorListenerany{}This method can be called in any state and calling it does not change the object state.setOnPreparedListenerany{}This method can be called in any state and calling it does not change the object state.setOnSeekCompleteListenerany{}This method can be called in any state and calling it does not change the object state.setScreenOnWhilePlayingany{}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.setWakeModeany{}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 theStarted 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 theStopped state. Calling this method in an invalid state transfers the object to the Error state.getTrackInfo{Prepared, Started, Stopped, Paused, PlaybackCompleted}{Idle, Initialized, Error}Successful invoke of this method does not change the state.addTimedTextSource{Prepared, Started, Stopped, Paused, PlaybackCompleted}{Idle, Initialized, Error}Successful invoke of this method does not change the state.selectTrack{Prepared, Started, Stopped, Paused, PlaybackCompleted}{Idle, Initialized, Error}Successful invoke of this method does not change the state.deselectTrack{Prepared, Started, Stopped, Paused, PlaybackCompleted}{Idle, Initialized, Error}Successful invoke of this method does not change the state.4. 参考文献
[1] Android MediaPlayer API http://developer.android.com/reference/android/media/MediaPlayer.html
- [MediaPlayer] Android MediaPlayer
- Android MediaPlayer
- android mediaplayer
- android mediaplayer
- Android MediaPlayer
- android MediaPlayer
- Android MediaPlayer .
- android MediaPlayer
- Android MediaPlayer
- Android MediaPlayer
- Android MediaPlayer .
- Android mediaplayer
- android:MediaPlayer
- android MediaPlayer
- android MediaPlayer
- Android MediaPlayer
- Android MediaPlayer
- Android mediaplayer
- 【LeetCode】Max Points on a Line
- HDU2489 Minimal Ratio Tree
- 进程间的通讯(IPC)方式
- cvSetImageROI 和 cv::Mat.copyTo 和 addWeighted
- java字符编码转换研究
- Android MediaPlayer
- 在命令行获取标准输入序列的反互序列,pep序列和长度信息
- 南京网站建设谈如何让网站高端大气上档次
- NodeJS中将回调函数转换成Promise的例子
- lpc23xx时钟和外部中断学习
- IOS学习 Custom iOS Target Properties
- 软件的各种版本
- 如何在Android实现桌面清理内存简单Widget小控件
- hive SQL 基础(转)