Android(安卓)原生 MediaPlayer 和 MediaCodec 的区别和联系(二)
16lz
2021-12-11
目录:
(3)Android 官方网站 对 MediaPlayer的介绍
正文:
Android 官方网站 对 MediaPlayer的介绍
MediaPlayer public class MediaPlayer extends Object implements VolumeAutomation, AudioRouting MediaPlayer类被用来控制音/视频文件和流的播放。可以在VideoView中找到有关如何使用此类中的方法的示例。 这里涉及的主题是: 1. 状态图 2. 有效和无效状态 3. 权限 4. 注册信息和错误回调 ★ 开发者指南 有关如何使用MediaPlayer的更多信息,请阅读 Media Playback开发人员指南。 状态图 音/视频文件和流的播放控制是作为一个状态机来进行管理。下图显示了受支持的播放控制操作驱动的MediaPlayer对象的生命周期和状态。 椭圆表示MediaPlayer对象可能驻留的状态。弧表示驱动对象状态转换的播放控制操作。有两种类型的弧。 单箭头的弧表示同步方法调用,而双箭头的弧表示异步方法调用。 从这个状态图中,可以看到MediaPlayer对象具有以下状态:- 当使用 new 创建MediaPlayer对象或者在调用 reset() 之后,它处于空闲状态; 并且在调用 release() 之后,它处于 End 状态。 在这两个状态之间是MediaPlayer对象的生命周期。
- 通常,一些播放控制操作可能由于各种原因而失败,例如不支持的音频/视频格式,交错的音频/视频,分辨率太高,流超时等。因此,在这些情况下,关注错误报告和恢复是非常重要的。有时,由于编程错误,也可能在无效状态下调用播放控制操作。在所有这些错误条件下,如果开发者事先通过setOnErrorListener(android.media.MediaPlayer.OnErrorListener)注册了 OnErrorListener ,则内部播放器引擎会调用开发者提供的 OnErrorListener.onError() 方法。
- 调用 setDataSource(FileDescriptor), 或 setDataSource(String), 或 setDataSource(Context, Uri), 或 setDataSource(FileDescriptor, long, long), 或 setDataSource(MediaDataSource)
- 在开始播放之前,MediaPlayer 对象必须先进入准备状态。
- 要开始播放,必须调用 start() ,start() 返回成功后,MediaPlayer对象则处于 Started状态(Started state)。isPlaying()可用来测试 MediaPlayer对象是否处于 Started状态(Started state)。
- 播放可以暂停和停止,并可以调整当前播放位置。 可以通过pause()暂停播放。 当对pause()的调用返回时,MediaPlayer对象进入Paused状态(Pausedstate)。 请注意,从“已启动”状态(Started state)到“暂停”状态(Paused state)的转换(反之亦然)在播放器引擎中异步发生。 在调用isPlaying()时更新状态可能需要一些时间,对于流内容,它可能需要几秒钟。
- 调用stop()会停止播放并导致处于Started,Paused,Prepared或PlaybackCompleted状态(state)的MediaPlayer进入Stopped状态(Stopped state)。
- 可以通过调用seekTo(long, int)调整播放位置
- 当播放到达流的结尾时,播放完成。
Method Name | Valid Sates | Invalid States | Comments |
attachAuxEffect | {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} | {Idle, Error} | This method must be called after setDataSource. Calling it does not change the object state. |
getAudioSessionId | any | {} | 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 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, PlaybackCompleted} | {Idle, Initialized, Prepared, Stopped, 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. |
setAudioAttributes | {Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted} | {Error} | Successful invoke of this method does not change the state. In order for the target audio attributes type to become effective, this method must be called before prepare() or prepareAsync(). |
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 (deprecated) | {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(). |
setAuxEffectSendLevel | any | {} | 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 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. |
setSurface | any | {} | 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 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. |
setPlaybackParams | {Initialized, Prepared, Started, Paused, PlaybackCompleted, Error} | {Idle, Stopped} | This method will change state in some cases, depending on when it's called. |
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. |
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. |
转载于:https://www.cnblogs.com/nmj1986/p/9307588.html
更多相关文章
- Android(安卓)四大组件之 Activity(1)--生命周期
- android电话服务
- Android绘制文本对象FontMetrics介绍及繪製文本
- Android不支持web页滚动
- Android调用系统摄像头拍照并剪裁压缩
- Android(安卓)屏幕旋转时保存状态
- ViewModel、ViewModelProviders、ViewModelStores官网学习总结
- android 状态栏和导航栏(status and navigation bars)
- android中的状态栏