SamRowley
SamRowley

Reputation: 3485

Seamless video Loop with VideoView

I have the following code to take a video as a raw resource, start the video and loop it but I need the video to loop seamlessly as of now when it comes to an end of the clip and starts the clip again the transition between causes a flicker for a split second, which I really can't have for my app.

public class Example extends Activity {
    VideoView vv;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        vv = (VideoView)findViewById(R.id.VideoView01);

        //Video Loop
        vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                vv.start(); //need to make transition seamless.
            }
        });

        Uri uri = Uri.parse("android.resource://com.example/"
                + R.raw.video);

        vv.setVideoURI(uri);
        vv.requestFocus();    
        vv.start();
    }
}

The clip is only 22 seconds long but was created to be seamless so it is possible to work without the delay.

Upvotes: 105

Views: 76809

Answers (8)

Chirag Kalra
Chirag Kalra

Reputation: 528

In Kotlin simply use

videoView.setOnPreparedListener { it.isLooping = true }

Upvotes: 20

Hitesh Sahu
Hitesh Sahu

Reputation: 45160

If you are using Kotlin

 videoView.setOnPreparedListener(object : MediaPlayer.OnPreparedListener {
                override fun onPrepared(mp: MediaPlayer?) {
                    //Start Playback
                    videoView.start()
                    //Loop Video
                    mp!!.isLooping = true;
                    Log.i(TAG, "Video Started");
                }
            });

Using Arrow Expression short form

videoView.setOnPreparedListener { mp ->
            //Start Playback
            videoView.start()
            //Loop Video
            mp!!.isLooping = true;
            Log.i(TAG, "Video Started");
        };

Upvotes: 1

PravinDodia
PravinDodia

Reputation: 3290

Try this it will work 100%


VideoView videoView;<---write this in outside of method or else declare it as final variable.

videoView.setOnPreparedListener(new OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.setLooping(true);
    }
});

Upvotes: 258

lexodus k
lexodus k

Reputation: 95

Not sure if this helps years later, but I used

vv.start();
vv.setOnCompletionListener ( new MediaPlayer.OnCompletionListener() {

 @Override 
  public void onCompletion(MediaPlayer mediaPlayer) {   
    vv.start();
  }
});

and it has a seamless loop

Upvotes: 9

ylmzekrm1223
ylmzekrm1223

Reputation: 103

Here is answer friends, you must use vv.resume in setOnCompletionListener class

[https://stackoverflow.com/a/27606389/3414469][1]

Upvotes: 0

J J
J J

Reputation: 1600

Little late, but any reason that you can't use the following?

MediaPlayer.setLooping(true);

Upvotes: 2

SamRowley
SamRowley

Reputation: 3485

Answer to this is to remove the audio from the video and convert that to a .ogg file which can be looped seamlessly and then use the video without audio to loop round and this works.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007624

The pause is for the underlying MediaPlayer to refresh its buffers. How long that will take will depend on a number of factors, many of which are outside your control (e.g., speed of CPU, speed of on-board flash storage).

One you can control is to get your video out of the resource and into the filesystem. Resources are stored in the APK, which is a ZIP file, so extracting the video this way probably takes extra time.

You may need to switch away from VideoView and use a SurfaceView with two MediaPlayers, alternating between them -- one is playing while the next is preparing, so when the playing one ends you can switch to the new player. I have not tried this, and so I do not know what the ramifications might be. However, I know that this technique is frequently used for audio playback to transition from one clip to another.

Upvotes: 5

Related Questions