MediaPlayer.seekTo() not seeking to position on Android

假如想象 提交于 2019-12-12 09:38:31

问题


I'm working on an app in which the video is paused at 3 different intervals. After the second pause, if a button is clicked, it should start back from the previous location.

Eg. if it is currently paused at 1:30, then on click of a button, it goes to the previous bookmark, i.e. 00:45.

I thought using MediaPlayer.seekTo() will help me achieve this. But, seekTo() doesn't seek the position at all. The currentPosition stays the same even after a call to seekTo();

Here's my code.

mediaPlayer.setOnSeekCompleteListener(new OnSeekCompleteListener() {
    @Override
    public void onSeekComplete(MediaPlayer mp) {
        Log.d("VID_PLAYER","Seek Complete. Current Position: " + mp.getCurrentPosition());
        mp.start();
    }
});

and somewhere below, I have this...

mediaPlayer.seekTo(45000);

What is the problem? Why isn't seekTo(); working? Any help would be appreciated.

I am currently testing it on Android v4.0.3 (ICS)


回答1:


One of the reasons why Android is not able to do seekTo is because of strange encoding of the videos. For example in MP4 format so called "seek points" (i.e. search index) can be specified at the begining and at the end of the file. If they are specified at the begining of the file then seekTo will behave correctly. But if they are specified at the end of the file then seekTo will do nothing and video will start from the begining after seekTo.

This is confirmed bug-or-feature in Android 4.4.2.

P.S. On Youtube all videos are encoded with "seek points" at the begining of the file. So if you have this problem with seekTo in your app first of all check your app with some video files from Youtube. Perhaps you'll need to reencode your videos...




回答2:


Does your problem have something to do with this bug: https://code.google.com/p/android/issues/detail?id=4124

I recall encountering this about a year ago. I don't think I found a workaround at the time.




回答3:


Since API 26, Android added a

seekTo(long msec, int mode);

By specifying a mode, we are able to tell to our MediaPlayer how to seek:

  • SEEK_PREVIOUS_SYNC: Has the same behavior as seekTo(int msec). It looks for the nearest 'seek point' (i.e. sync frame) backwards.
  • SEEK_NEXT_SYNC: Same as PREVIOUS, but looks forwards.
  • SEEK_CLOSEST_SYNC: This looks for the nearest sync frame given a msec time.
  • SEEK_CLOSEST: This seeks for the nearest frame given a msec time.

I faced a similar problem a couple days ago and by using SEEK_CLOSEST mode, this problem was solved.

For reference, check out these links: https://developer.android.com/reference/android/media/MediaPlayer.html#seekTo(long,%20int)

https://en.wikipedia.org/wiki/Video_compression_picture_types



来源:https://stackoverflow.com/questions/16077026/mediaplayer-seekto-not-seeking-to-position-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!