Android -- 多媒体播放之MediaPlayer基础简介
Android -- MediaPlayer基础简介
MediaPlayer是Android中的一个多媒体播放类,我们能通过它控制音视频流或本地音视频资源的播放过程。
这段时间查阅了Android官方文档对MediaPlayer的介绍,同时也看了一些书籍介绍。现在把MediaPlayer的基础内容在此做一个记录,以供后面查阅、回忆。
MediaPlayer系统的主要代码实现介绍如下:
MediaPlayer.java
- 当一个MediaPlayer对象被刚刚用new操作符或者是调用了reset()方法后,就处于Idle状态。当调用了release()方法后,处于End状态。这两种状态之间是MediaPlayer对象的生命周期。
方法 | 有效状态 | 无效状态 | 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 Preparedstate. 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 Preparingstate. 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 | {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 Initializedstate. 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. |
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. |
在播放过程中,应用程序也许会想去注册一些回调函数来获取一些内部状态更新或运行错误的事件信息。所有这些都可以通过正确的设置一些监听对象来实现:
setOnPreparedListener(OnPreparedListener)
setOnPreparedListener,
setOnVideoSizeChangedListener(OnVideoSizeChangedListener)
setOnVideoSizeChangedListener,
setOnSeekCompleteListener(OnSeekCompleteListener)
setOnSeekCompleteListener,
setOnCompletionListener(OnCompletionListener)
setOnCompletionListener,
setOnBufferingUpdateListener(OnBufferingUpdateListener)
setOnBufferingUpdateListener,
setOnInfoListener(OnInfoListener)
setOnInfoListener,
setOnErrorListener(OnErrorListener)
setOnErrorListener, etc
为了能通过这些监听器正确收到相关联的回调,在创建MediaPlayer对象时,要确保它所在的线程必须有自己的消息循环(Looper)。
MediaPlayer中的内嵌类 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
interface | MediaPlayer.OnBufferingUpdateListener | Interface definition of a callback to be invoked indicating buffering status of a media resource being streamed over the network. | |||||||||
interface | MediaPlayer.OnCompletionListener | Interface definition for a callback to be invoked when playback of a media source has completed. | |||||||||
interface | MediaPlayer.OnErrorListener | Interface definition of a callback to be invoked when there has been an error during an asynchronous operation (other errors will throw exceptions at method call time). | |||||||||
interface | MediaPlayer.OnInfoListener | Interface definition of a callback to be invoked to communicate some info and/or warning about the media or its playback. | |||||||||
interface | MediaPlayer.OnPreparedListener | Interface definition for a callback to be invoked when the media source is ready for playback. | |||||||||
interface | MediaPlayer.OnSeekCompleteListener | Interface definition of a callback to be invoked indicating the completion of a seek operation. | |||||||||
interface | MediaPlayer.OnTimedTextListener | Interface definition of a callback to be invoked when a timed text is available for display. | |||||||||
interface | MediaPlayer.OnVideoSizeChangedListener | Interface definition of a callback to be invoked when the video size is first known or updated | |||||||||
class | MediaPlayer.TrackInfo | Class for MediaPlayer to return each audio/video/subtitle track's metadata. |
Android -- 多媒体播放之MediaPlayer基础简介相关推荐
- Android 自动化测试 Espresso篇:简介基础使用
前言 最近在尝试深入学习Android单元测试. 以前笔者对于单元测试的理解很简单,做一个简单的API测试获取Server端数据,或者测试对应简单的工具类和数据处理的逻辑. 经过这近一个月的碎片化学习 ...
- 快速接入 Android BLE 开发的基础框架
代码地址如下: http://www.demodashi.com/demo/12092.html * Android BLE基础操作框架,基于回调,操作简单.包含扫描.多连接.广播包解析.服务读写及通 ...
- 第一章 android以及智能手机行业相关简介
第一章 android以及智能手机行业相关简介读后感受&知识累计 最近深刻了解到自己基础知识的不足,变不断的开始阅读很多android相关的知识,希望这些对本人未来的职业发展有很大的帮助.下面 ...
- Android 编译之make基础(转)
Android 编译之make基础 原文链接 qiuxintai 0.498 2020.10.14 18:52:40 字数 13,246 阅读 2,631 引言 做android系统相关的开发已经有几 ...
- Android音视频开发基础(七):视频采集-系统API基础
前言 在Android音视频开发中,网上知识点过于零碎,自学起来难度非常大,不过音视频大牛Jhuster提出了<Android 音视频从入门到提高 - 任务列表>.本文是Android音视 ...
- Android DRM框架与基础知识
Android DRM框架与基础知识 Android DRM框架 DRM框架的目的:能让安卓设备可以播放更多的内容,不同的内容和硬件设备可能使用的是不同的内容版权保护机制或者没有版权管理机制,但是安卓 ...
- android java框架_Android 框架简介:java框架
Android 框架简介:java框架 2015-10-15 14:43 来源: 正保IT教育网整理 这里简单的介绍了Android的java环境基础,在后面一节中会结合具体的实例来理解这一节的内容 ...
- Android ble连接过程,Android BLE的一些基础知识及相关操作流程 - 全文
前言: 本文主要描述Android BLE的一些基础知识及相关操作流程,不牵扯具体的业务实现,其中提供了针对广播包及响应包的解析思路,希望对正在或即将面临Android BLE开发的伙伴们有所引导. ...
- Android多媒体播放器源码解析(stagefright框架)
一.android多媒体播放的调用步骤: a) android中定义一个MediaPlayer类 b) 在MediaPlayer中使用JNI技术调用的是android_media_MediaPlaye ...
最新文章
- 使用Intellij Idea 创建一个Web项目!
- Vue父子组件通信小总结
- ipad写python_ipad怎么写python
- Javascript 面向对象编程初探(一)--- 封装
- linux简单快速启用web
- php function split is deprecated,如何解决php Function split() is deprecated 的问题
- spring boot (3.自动配置@enableautoconfiguration)
- 当C++爬山壁纸——C++山寨版
- 中国软件开发project师之痛
- 2021-2025年中国制药行业MR报告软件行业市场供需与战略研究报告
- 计算机与管理科学的交叉与融合,计算机信息管理学科交叉融合研究论文
- Javamysql语法转化oracle_MyBatis项目实战 快速将MySQL转换成Oracle语句
- 118. PHP 性能问题
- linux 配置 NTP 服务器
- 百科知识:呼叫转移与呼叫前转
- 搜索技巧——奇偶剪纸
- npm install报错解决fatal: Unable to look up github.com (port 9418) npm ERR! exited with error code: 128
- win10 c语言 语音功能,win10系统自带录音功能在哪?windows10开启录音功能的方法-系统城...
- 4k显示服务器,远程服务器4k显示器
- MySQL42000错误
热门文章
- 注解 @TableField(exist = false) 注明非数据库字段属性
- 基于RBF函数的点云孔洞修补
- 微信公众号开发之获取用户列表和用户基本信息(五)
- OpenGL 2D贴图texture与着色器Shader
- IMEI号和MEID号
- Qt Tablewidget表格数据的导出和导入
- 二值图像的形态学处理
- (DCDC)CM3601降压型开关稳压器 60V,480KHz,0.5A
- linux如何安装vmtools工具
- 今天高兴,分享一个我亲自测试的网上赚钱方法......