Search code examples
androidandroid-mediaplayerandroid-videoview

VideoView and MediaPlayer seekTo always starts from beginning


I have some problem with seeking video played in VideoView and resuming it.

Here's some part of my code:

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();
        mp.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
            @Override
            public void onSeekComplete(MediaPlayer mp) {
                Log.d("V", "onSeekComplete1 " + videoView.getCurrentPosition());
                videoView.start();
                Log.d("V", "onSeekComplete1 " + videoView.getCurrentPosition());
            }
        });
    }
});
videoView.setVideoURI('some local uri');

At some point I'm pausing video using: videoView.pause();

After button click I'm seeking video to certain time, eg. 10 seconds (10 000 ms).

@OnClick(R2.id.ivMove)
void clickMove() {
   videoView.seekTo(10000);
}

After that in log I see:

onSeekComplete1 10000
onSeekComplete2 0

So after calling videoView.start() it is starting video from beginning!

But when I call start() without seeking video (eg. one button calls pause() and second calls start() then video is playing correctly - from the moment when it was paused, not from beginning.

Could you tell me what I'm doing wrong?


Solution

  • The problem was caused by MediaPlayer - it is seeking to closest keyframe of the video and it turns out that my testing video has keyframes every ~30 seconds.

    I changed to ExoPlayer https://github.com/google/ExoPlayer - it can seek to exact time.