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 p