开发者

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 acce开发者_StackOverflow社区ss 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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜