android使用 mediaPlayer 播放video视频过程中, 当用户退出当前播放,再从后台恢复播放时,需要跳转到之前退出的时间点继续播放。

使用的方法基本都是 SeekTo 之前的时间点,但是经常遇到恢复播放时位置不准的问题,而且甚至有重头开始播放的现象。这个是因为SeekTo是回到上一时间点附近的关键帧导致的。

针对这个问题,在最新的android 8.0平台上,已经有了新的解决方案:

SeekTo() 方法在android O 平台新增了一个多参数的方法:

void seekTo(long msec, @SeekMode int mode)

这里的Mode 传入

int SEEK_CLOSEST = 0x03

就不会出现播放视频位置不准确的现象了。

/*** Seek modes used in method seekTo(long, int) to move media position* to a specified location.** Do not change these mode values without updating their counterparts* in include/media/IMediaSource.h!*//*** This mode is used with {@link #seekTo(long, int)} to move media position to* a sync (or key) frame associated with a data source that is located* right before or at the given time.** @see #seekTo(long, int)*/public static final int SEEK_PREVIOUS_SYNC    = 0x00;/*** This mode is used with {@link #seekTo(long, int)} to move media position to* a sync (or key) frame associated with a data source that is located* right after or at the given time.** @see #seekTo(long, int)*/public static final int SEEK_NEXT_SYNC        = 0x01;/*** This mode is used with {@link #seekTo(long, int)} to move media position to* a sync (or key) frame associated with a data source that is located* closest to (in time) or at the given time.** @see #seekTo(long, int)*/public static final int SEEK_CLOSEST_SYNC     = 0x02;/*** This mode is used with {@link #seekTo(long, int)} to move media position to* a frame (not necessarily a key frame) associated with a data source that* is located closest to or at the given time.** @see #seekTo(long, int)*/public static final int SEEK_CLOSEST          = 0x03;/** @hide */@IntDef(value = {SEEK_PREVIOUS_SYNC,SEEK_NEXT_SYNC,SEEK_CLOSEST_SYNC,SEEK_CLOSEST,})@Retention(RetentionPolicy.SOURCE)public @interface SeekMode {}private native final void _seekTo(long msec, int mode);/*** Moves the media to specified time position by considering the given mode.* <p>* When seekTo is finished, the user will be notified via OnSeekComplete supplied by the user.* There is at most one active seekTo processed at any time. If there is a to-be-completed* seekTo, new seekTo requests will be queued in such a way that only the last request* is kept. When current seekTo is completed, the queued request will be processed if* that request is different from just-finished seekTo operation, i.e., the requested* position or mode is different.** @param msec the offset in milliseconds from the start to seek to.* When seeking to the given time position, there is no guarantee that the data source* has a frame located at the position. When this happens, a frame nearby will be rendered.* If msec is negative, time position zero will be used.* If msec is larger than duration, duration will be used.* @param mode the mode indicating where exactly to seek to.* Use {@link #SEEK_PREVIOUS_SYNC} if one wants to seek to a sync frame* that has a timestamp earlier than or the same as msec. Use* {@link #SEEK_NEXT_SYNC} if one wants to seek to a sync frame* that has a timestamp later than or the same as msec. Use* {@link #SEEK_CLOSEST_SYNC} if one wants to seek to a sync frame* that has a timestamp closest to or the same as msec. Use* {@link #SEEK_CLOSEST} if one wants to seek to a frame that may* or may not be a sync frame but is closest to or the same as msec.* {@link #SEEK_CLOSEST} often has larger performance overhead compared* to the other options if there is no sync frame located at msec.* @throws IllegalStateException if the internal player engine has not been* initialized* @throws IllegalArgumentException if the mode is invalid.*/public void seekTo(long msec, @SeekMode int mode) {if (mode < SEEK_PREVIOUS_SYNC || mode > SEEK_CLOSEST) {final String msg = "Illegal seek mode: " + mode;throw new IllegalArgumentException(msg);}// TODO: pass long to native, instead of truncating here.if (msec > Integer.MAX_VALUE) {Log.w(TAG, "seekTo offset " + msec + " is too large, cap to " + Integer.MAX_VALUE);msec = Integer.MAX_VALUE;} else if (msec < Integer.MIN_VALUE) {Log.w(TAG, "seekTo offset " + msec + " is too small, cap to " + Integer.MIN_VALUE);msec = Integer.MIN_VALUE;}_seekTo(msec, mode);}

android MediaPlayer 的SeekTo 方法相关推荐

  1. android seekto实现_「seekto」android MediaPlayer 的SeekTo 方法 - seo实验室

    seekto Android使用 mediaplayer 播放video视频过程中, 当用户退出当前播放,再从后台恢复播放时,需要跳转到之前退出的时间点继续播放. 使用的方法基本都是 seekto 之 ...

  2. oracle prepare耗时,Android MediaPlayer的prepare()方法太耗时问题

    如题,本人用MediaPlayer播放网网络上的mpg视频,先执行mediaPlayer.prepare()方法再start(),但播放有的视频,这个prepare()方法会执行几十秒后才会执行sta ...

  3. mediaPlayer的seekto方法

    最近工作的任务与播放器相关(本人是个新手),需要用到mediaPlayer这个类的一些常用方法.在实现快进后退的时候需要用到seekto这个方法,我百度查了一下对这个方法的介绍不是太多.后来经过实践才 ...

  4. Android MediaPlayer+SurfaceView播放视频 (异常处理)

    MediaPlayer,顾名思义是用于媒体文件播放的组件.Android中MediaPlayer通常与SurfaceView一起使用,当然也可以和其他控件诸如TextureView.SurfaceTe ...

  5. android mediaplayer 实现歌曲边播放边下载

    做音乐播放器,有时候会用到系统自带的mediaplayer播放器,这个播放器底层是在linux上面,封装了一些api供使用者调用,由于网络HTTP请求歌曲流这一块的都已经被封装了,所以要想实现歌曲边下 ...

  6. Android笔记之seekTo

    有时候我们在播放视频的时候需要对视频进行seekTo处理.通过videoPlayer或者mediaPlayer的seekTo方法理论上是可以简单的实现. public native void seek ...

  7. 【Android 多媒体开发】 MediaPlayer 状态机 接口 方法 解析

    作者 : 韩曙亮 转载请著名出处 :  http://blog.csdn.net/shulianghan/article/details/38487967 一. MediaPlayer 状态机 介绍 ...

  8. android播放mp3方法,Android MediaPlayer 播放音频的方式

    主要介绍使用MediaPlayer播放音频的方式.关于MediaPlayer的基础知识,比如状态,可以参考Android MediaPlayer基础简介. 为了方便表达,定义变量名为mediaPlay ...

  9. android seekto实现_MediaExtractor的seekTo方法精确定位到指定帧

    MediaExtractor有一个方法如下   http://developer.android.com/intl/es/reference/android/media/MediaExtractor. ...

最新文章

  1. python turtle画滑稽_使用python的turtle函数绘制一个滑稽表情的方法
  2. 三星为何要在安卓之外开发 Tizen 系统?
  3. 如何替换所有出现的字符串?
  4. 2_flutter_TextField(文本框),TabBar(选项卡),bottomNavigationBar(底部导航栏)
  5. win10 安装程序错误,the setup files are corrupted.please obtain a new copy of the program
  6. 第二轮冲次会议第七次
  7. Cowboy 源码分析(十八)
  8. sap中泰国有预扣税设置吗_泰国的绘图标志| Python中的图像处理
  9. 思维革命:交换两个变量的值
  10. 商汤科技大涨逾15% 市值突破2000亿港元
  11. ftp可以传输什么类型文件_为什么文件传输软件总让数据“没有安全感”?
  12. 想成为一个高效的Web开发者吗?来看看大牛分享的经验吧
  13. 加盟商最大的顾虑是什么?
  14. linux中dir命令,详解Linux系统中ls和dir命令的组合使用
  15. ESP32 GPIO入门之闪灯
  16. 华为首款血压手表WATCH D测评
  17. VC 2012 visualstudio的项目属性表 .props文件
  18. 软件开发入门【3分钟课程】
  19. Downward paths
  20. 工测中坐标方位角的计算c语言,工程测量中坐标方位角是怎么进行推算的?

热门文章

  1. C语言metropolis方法,详解R语言MCMC:Metropolis-Hastings采样用于回归的贝叶斯估计
  2. 彻底搞定C语言指针详解完整版
  3. 黑盒测试和白盒测试区别
  4. 开车的十大误区,超级有用
  5. JS + CSS 做一个简易九宫格抽奖
  6. 2021 栉风沐雨 , 砥砺前行
  7. JavaScript:打印五行五列星星
  8. zmq系列--Router和Dealer模式
  9. 中国文联致信祝贺莫言获得诺贝尔文学奖-文联-祝贺-莫言
  10. 网站策划常识[转载]