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

2019-02-04 01:37发布

问题:

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

回答1:

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);
    }
});