Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Video View: how to play a video on a loop

I got a simple dialog box with a VideoView in it and I want to play the video in a loop.

I'm currently using a quick fix

 //Video Loop         vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {             public void onCompletion(MediaPlayer mp) {                 vv.start();             }         }); 

but I would like to know if there is a better way?


Edit

I'm adding more code because I don't know how I can get access to the MediaPlayer object form the VideoView:

String path = defaultPath+currentVideoRessource;       if (path == null || path.length() == 0) {         Log.e("extra","File URL/path is empty");     } else {         // If the path has not changed, just start the media player         if (!path.equals(current) && mVideoView != null) {                 Uri pathURI = Uri.parse(defaultPath+currentVideoRessource);                 mVideoView.setVideoURI(pathURI);     }     current = path;     mVideoView.setOnCompletionListener(new MyOnCompletionListener(this));     //Video Loop     //              mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {     //                  public void onCompletion(MediaPlayer mp) {     //                      mVideoView.start(); //need to make transition seamless.     //                  }     //              });      mVideoView.start();     mVideoView.requestFocus(); 

I'm currently looking into using directly MediaPlayer and a SurfaceView bu I would like to know if there is a way with VideoView directly

like image 298
Jason Rogers Avatar asked Apr 06 '11 16:04

Jason Rogers


People also ask

How do you get videos to loop?

YouTube gives us a very easy solution to play a video on repeat mode. To make a video play on a loop, do a right-click on the video and select 'loop' from the options.


2 Answers

Use setLooping(true) on your MediaPlayer instance.

--Edit--

How about using setOnPrepareListener instead of setOnCompletionListener? This gives you access to the MediaPlayer object.

vv.setOnPreparedListener (new OnPreparedListener() {                         @Override     public void onPrepared(MediaPlayer mp) {         mp.setLooping(true);     } }); 
like image 196
Marc Bernstein Avatar answered Oct 12 '22 03:10

Marc Bernstein


You Can take reference form below codes, Where setup_welcome_video is video file.

        myVideo = findViewById(R.id.VideoView);         Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.setup_welcome_video);         myVideo.setVideoURI(uri);         myVideo.start();         myVideo.requestFocus();         myVideo.setOnPreparedListener (mp -> mp.setLooping(true)); 
like image 21
Jha Chandan Avatar answered Oct 12 '22 03:10

Jha Chandan