Android:视频观看:如何在循环播放视频

Jas*_*ers 33 video android android-videoview

我有一个简单的对话框,里面有一个VideoView,我想循环播放视频.

我目前正在使用快速修复程序

 //Video Loop
        vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                vv.start();
            }
        });
Run Code Online (Sandbox Code Playgroud)

但我想知道是否有更好的方法?


编辑

我正在添加更多代码,因为我不知道如何从以下形式访问MediaPlayer对象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();
Run Code Online (Sandbox Code Playgroud)

我正在考虑直接使用MediaPlayerSurfaceView我想知道是否有VideoView直接的方法

Mar*_*ein 81

在MediaPlayer实例上使用setLooping(true).

- 编辑 -

如何使用setOnPrepareListener而不是setOnCompletionListener?这使您可以访问MediaPlayer对象.

vv.setOnPreparedListener (new OnPreparedListener() {                    
    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.setLooping(true);
    }
});
Run Code Online (Sandbox Code Playgroud)


小智 5

您可以参考下面的代码,其中 setup_welcome_video 是视频文件。

        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));
Run Code Online (Sandbox Code Playgroud)