How To Make Android VideoView Seekto Method Consistent

In previous article we have introduced how to use android VideoView widget to play, pause, continue, stop and replay video file in android. But when you use VideoView’s seekto method to make the video play from a particular position, you will find the video playing is inconsistent. Some times it start from the beginning.

There are two methods to resolve above problem.

1. Make Video Start After SeekTo Method Complete.

This is one reason for the issue. The VidewView widget start to play video file while seekto method do not finish.

You can add a MediaPlayer.OnPreparedListener to the VideoView object. And override it’s onPrepared method to resolve this problem.

From onPrepared method we can get a reference to MediaPlayer object. And we can set MediaPlayer.OnSeekCompleteListener to listen to the seek complete event and play the video when the seek action complete.

Why we do above action is because we can not set OnSeekCompleteListener to VideoView object directly. So we have to do like this. Now the video will play only when the seekto method complete.

playVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {

        // Set media player on seek complete listener.
        mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {

            // This method will be invoked when seekto method complete.
            @Override
            public void onSeekComplete(MediaPlayer mediaPlayer) {

                // Play video only when seekto method complete and video is paused.
                if(isVideoPaused)
                {
                    playVideoView.start();
                    isVideoPaused = false;
                }
            }
        });
    }
});

2. Use FFMpeg To Make Video Key Frame Continuous.

Video file consists of frames, and there are some frame that is called key frame. When you use seekto method to seek to special position, the VideoView object will jump to the key frame which is nearest to the position.

READ :   Android Progress Dialog Example

So if the key frame in the video is not continuous, you will find the seekto method do not consistent. So we need a tool to make the video key frame continuous.

FFMpeg is a very good tool to do above task. First you need to download it from https://ffmpeg.zeranoe.com/builds/. This is a windows version. You can also download the souce code from it’s official website since it is open source.

After download, unzip it to a local folder such as C:\ffmpeg, and add C:\ffmpeg\bin folder in the computer Path environment variable. Please refer article How To Set Java Environment Viriable JAVA_HOME, CLASSPATH, PATH for detail explanation.

Then open dos window, run below command in it. This command will make a key frame between every 2 normal frame. Then you will find VideoView seekto method more accurate and consistent with the new key frame added video file.

ffmpeg.exe -i "C:\WorkSpace\play_video_test.mp4" -c:v libx264 -preset superfast -x264opts keyint=2 -acodec copy -f mp4 "C:\WorkSpace\play_video_test_key_frame.mp4"

use ffmpeg to add continuous key frame in video file

Reference

  1. Android Play Video File From Local SD Card / Web Example
(Visited 846 times, 3 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.