1)如何获得MediaPlayer实例:
//-- Context context : Android 的上下文对象;
//-- Uri uri : 数据源的 Uri;
//-- SurfaceHolder holder : 指定要播放视频的 SurfaceHolder ;
public static MediaPlayer create(Context context, Uri uri, SurfaceHolder holder)
- 如何设置要播放的⽂件:
public void setDataSource(@NonNull Context context, @NonNull Uri uri)
3)对播放器的主要控制⽅法:
Android通过控制播放器的状态的⽅式来控制媒体⽂件的播放,其中:
prepare()
public void prepare() throws IOException, IllegalStateException {
和prepareAsync()
public native void prepareAsync() throws IllegalStateException;
提供了同步和异步两种⽅式设置播放器进⼊prepare状态,需要注意的是,如果MediaPlayer实例是由
create⽅法创建的,那么第⼀次启动播放前不需要再调⽤prepare()了,因为create⽅法⾥已经调⽤过了。
start()是真正启动⽂件播放的⽅法。
public void start() throws IllegalStateException {
pause()和stop()⽐较简单,起到暂停和停⽌播放的作⽤,
public void pause() throws IllegalStateException {
private native void _stop() throws IllegalStateException;
- 获取当前位置
/**
* Gets the current playback position.
*
* @return the current position in milliseconds : 获取当前播放器播放的位置, 返回值是 已经播放了的毫秒数;
*/
public native int getCurrentPosition();
方法的有效状态和无效状态 :
//-- 有效状态 :Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, 在以上状态调用该方法不会改变 MediaPlayer 状态;
//-- 无效状态 : Error 状态, 在 Error 状态调用该方法, 会进入 Error 状态中;
- 获取文件时长
public native int getDuration();
- 获取视频高度 宽度
public native int getVideoHeight();
public native int getVideoWidth();
- 检查 MediaPlayer 是否在循环
public native boolean isLooping();
- 检查 MediaPlayer 是否在播放
public native boolean isPlaying();
4)MediaPlayer状态图
1。对播放音频/视频文件和流的控制是通过一个状态机来管理的。由一个箭头开始的弧代表同步的方法调用,而以双箭头开头的代表的弧线代表异步方法调用。
2.
– 生命周期开始 : 进入 Idle (闲置) 状态;
– 生命周期结束 : 进入 End (结束) 状态;
– 进入 Idle 状态 : MediaPlayer 刚被创建 new MediaPlayer() 或者 调用了 reset() 方法之后, 进入 Idle (闲置) 状态;
– 进入 End 状态: 在 Idle 状态调用 release() 方法后, 会进入 End (结束) 状态;
两种进入 Idle 状态方法的差别: 在 Idle 状态无法调用 getCurrentPosition(), getDuration(), getVideoHeight(), getVideoWidth(), setAudioStreamtype(int), setLooping(boolean), setVolume(float, float), pause(), start(), stop(), seekTo(), prepare(), prepareAsync() 方法都是错误的;
– new MediaPlayer() 进入 Idle 状态 : 此时 MediaPlayer 内部引擎 和 状态都没有改变, 调用上面的方法之后, 将 无法调用 OnErrorListener.onError() 方法;
– reset() 进入 Idle 状态 : 此时如果调用上面的方法, 内部的引擎就会 回调 OnErrorListener.onError() 方法;
创建 和 重载 MediaPlayer 区别:
– 创建 MediaPlayer: 通过 new MediaPlayer() 创建的对象处于 Idle (闲置) 状态;
– 重载 MediaPlayer : 通过 create() 方法创建的 MediaPlayer 对象处于 Prepare (准备) 状态;
End (结束) 状态解析:
–release() 方法作用: 该方法会释放 播放引擎 中与 MediaPlayer 相关的资源;
– 释放唯一性资源 : 有些资源如 硬件加速组件 单态组件等都是唯一性的资源, 如果不释放掉, 之后的 Mediaplayer 都无法正常运行;
– 无法进行状态转换 : End 状态代表 MediaPlayer 生命周期结束, 在此状态不能转换成其它状态了;
Prepared (就绪) 和 Preparing (准备中) 状态
– 从 Initialized 状态迁移 : 在 Initialized 状态调用 prepare() 方法, 如果方法成功返回, MediaPlayer 就会进入 Prepared 状态;
– 从 Preparing 状态迁移 : 在 Preparing 状态调用 OnPrepareListener.onPrepared() 方法迁移到 Prepared 状态;
Preparing (准备中) 状态: Initialized 状态调用 prepareAsync() 方法进入 Preparing 状态;
– 该状态执行的操作: 在 Preparing 状态时, 播放器引擎会继续完成准备工作, 同步版本返回 或者 异步版本准备工作完成就会调用 OnPrepareListener.onPrepared() 方法进入 Prepared 状态;
抛出异常 : 只有在 Initialized 方法中才能调用 prepare() 和 prepareAsync()方法, 在其它状态调用会报出 IllegalStateException 遗产;
Started 状态迁移: 在 Prepared 状态调用 start() 方法, MediaPlayer 即迁移到了 Started 状态;
– 判断 MediaPlayer 是否在 Started 状态: 在任何状态下调用 isPlaying() 方法, 可以判断 MediaPlayer 是否在 Started 状态;
– 跟踪缓冲状态 : 在 Started 状态, 调用 OnBufferingUpdateListener.onBufferingUpdate() 方法, 可以获取视频音频流的缓冲状态;
5.
Paused (暂停) 状态迁移: 在 Started 状态调用 pause() 方法, MediaPlayer 会进入 Paused 状态;
– 状态迁移时间: Started 状态转换为 Paused 状态需要一定时间, 这个过程是异步的, 过一段时间之后 isPlaying() 状态才会改变;
– 回到 Started 状态 : 在 Paused 状态调用 start() 方法, 会进入 Started 状态;
5)接口
1.缓冲相关接口
/**
* Interface definition of a callback to be invoked indicating buffering
* status of a media resource being streamed over the network.
*/
public interface OnBufferingUpdateListener
{
/**
* Called to update status in buffering a media stream received through
* progressive HTTP download. The received buffering percentage
* indicates how much of the content has been buffered or played.
* For example a buffering update of 80 percent when half the content
* has already been played indicates that the next 30 percent of the
* content to play has been buffered.
*
* @param mp the MediaPlayer the update pertains to
* @param percent the percentage (0-100) of the content
* that has been buffered or played thus far
*/
void onBufferingUpdate(MediaPlayer mp, int percent);
// -- 方法作用: 该方法在 MediaPlayer 通过 HTTP 下载缓冲视频流的时候回调, 用以改变视频缓冲状态;
//-- 方法参数: mp 即 MediaPlayer 实体对象; percent 已经缓冲了的 或者 播放了的 媒体流百分比;
}
接口作用: 定义一个回调接口, 该接口的作用是在流媒体缓冲状态发生改变的时候, 标明该状态;
2.播放完毕相关接口
/**
* Interface definition for a callback to be invoked when playback of
* a media source has completed.-- 接口作用: 在接口中定义了 流媒体 播放完毕后回调的方法;
*/
public interface OnCompletionListener
{
/**
* Called when the end of a media source is reached during playback.
*
* @param mp the MediaPlayer that reached the end of the file
*/
void onCompletion(MediaPlayer mp);//--方法作用: 在 媒体流 播放完毕之后回调;
}
3.信息相关接口
出现的错误类型:
MEDIA_ERROR_UNKONWN(位置错误) 或者
MEDIA_ERROR_SERVER_DIED(服务器错误) ;
—c. int extra: 针对与具体错误的附加码, 用于定位错误更详细信息, 例如
MEDIA_ERROR_IO(本地文件或网络相关错误),
MEDIA_ERROR_MALFORMAD(比特流不符合相关的编码标准和文件规范),
MEDIA_ERROR_UNSUPPORTED(框架不支持该功能),
MEDIA_ERROR_TIME_OUT(一些操作超时);
4.
/**
* Interface definition of a callback to be invoked to communicate some
* info and/or warning about the media or its playback.-- 方法作用 : 出现了信息或者警告的时候回调;
*/
public interface OnInfoListener
{
/**
* Called to indicate an info or a warning.
*
* @param mp the MediaPlayer the info pertains to.
* @param what the type of info or warning.
* <ul>
* <li>{@link #MEDIA_INFO_UNKNOWN}(未知的信息),
* <li>{@link #MEDIA_INFO_VIDEO_TRACK_LAGGING}(视频过于复杂解码太慢),
* <li>{@link #MEDIA_INFO_VIDEO_RENDERING_START}(开始渲染第一帧),
* <li>{@link #MEDIA_INFO_BUFFERING_START}暂停播放开始缓冲更多数据),
* <li>{@link #MEDIA_INFO_BUFFERING_END}(缓冲了足够的数据重新开始播放),
* <li><code>MEDIA_INFO_NETWORK_BANDWIDTH (703)</code> -
* bandwidth information is available (as <code>extra</code> kbps)
* <li>{@link #MEDIA_INFO_BAD_INTERLEAVING}错误交叉),
* <li>{@link #MEDIA_INFO_NOT_SEEKABLE}(媒体不能够搜索),
* <li>{@link #MEDIA_INFO_METADATA_UPDATE}(一组新的元数据用),
* <li>{@link #MEDIA_INFO_UNSUPPORTED_SUBTITLE}(不支持字幕),
* <li>{@link #MEDIA_INFO_SUBTITLE_TIMED_OUT}(读取字幕使用时间过长);
* </ul>
* @param extra an extra code, specific to the info. Typically
* implementation dependent.
* @return True if the method handled the info, false if it didn't.
* Returning false, or not having an OnInfoListener at all, will
* cause the info to be discarded.
*/
boolean onInfo(MediaPlayer mp, int what, int extra);
}
5。准备播放相关接口
/**
* Interface definition for a callback to be invoked when the media
* source is ready for playback.
*/--
// 接口作用 : 该接口中定义一个回调方法, 该方法在进入 Prepared 状态 并 开始播放的时候回调;
public interface OnPreparedListener
{
/**
* Called when the media file is ready for playback.
*
* @param mp the MediaPlayer that is ready for playback
*/
void onPrepared(MediaPlayer mp);
// -- 方法作用 :该方法在进入 Prepared 状态 并 开始播放的时候回调;
//-- 参数介绍 : MediaPlayer mp , MediaPlayer 实体对象;
}
这里是引用
https://wenku.baidu.com/view/9d8aea10edfdc8d376eeaeaad1f34693daef1085.html
https://blog.youkuaiyun.com/weixin_29415429/article/details/114102232