安卓:视频视图:如何循环播放视频

40

我有一个简单的对话框,其中包含一个VideoView,我想循环播放视频。

我目前正在使用一个快速解决方法

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

不过,我想知道是否有更好的方法?


编辑

我正在添加更多代码,因为我不知道如何从VideoView中获取MediaPlayer对象的访问:

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

我目前正在研究如何直接使用MediaPlayerSurfaceView。但我想知道是否有一种直接使用VideoView 的方法。


请尝试一下,这对我来说很明显有效。 [http://stackoverflow.com/a/27606389/3414469][1] - ylmzekrm1223
2个回答

92

在你的MediaPlayer实例上使用setLooping(true)方法。

--编辑--

使用setOnPrepareListener代替setOnCompletionListener如何?这将为您提供对MediaPlayer对象的访问。

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

请查看已编辑的答案,如果可以,请告诉我。 - Marc Bernstein
@Jason... 那你现在使用媒体播放器了吗?它是内联的吗?你能发一些代码吗?我一直在列表中使用videoviews来做这个,但滚动会导致一些渲染问题。谢谢。 - Anthony Graglia
以上代码对我没有起作用 -- 也许setLooping对流式视频没有影响? - Sven Viking
@MarcBernstein,它在循环中添加了几秒钟的暂停。如何在没有任何暂停的情况下播放? - MohsinSyd
@MarcBernstein 它适用于mp4视频(文件),但不能用于m3u8。如果您对m3u8文件有任何想法,请建议我一个解决方案。 - Maroti
显示剩余3条评论

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接