使用Android原生的MediaPlayer時調用默認的seekTo(long time)的時候,播放器會在設置的時間點往回退一點時間。查看源碼之后如下:
public void seekTo(int msec) throws IllegalStateException {
seekTo(msec, SEEK_PREVIOUS_SYNC /* mode */);
}
默認是調用了兩個參數的seekTo方法,第二個參數一共由四種mode,源碼中注釋也很清楚。
/**
* 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;
在系統大于等于8.0的手機上只要調用seekTo(time,SEEK_CLOSEST)的方法就可以解決問題了。