【问题标题】:Android: Video View: how to play a video on a loopAndroid:视频视图:如何循环播放视频
【发布时间】:2011-04-06 16:11:44
【问题描述】:

我有一个简单的对话框,里面有一个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 bu 我想知道是否有直接使用VideoView 的方法

【问题讨论】:

标签: android video android-videoview


【解决方案1】:

在您的 MediaPlayer 实例上使用 setLooping(true)

--编辑--

使用setOnPrepareListener 代替 setOnCompletionListener 怎么样?这使您可以访问 MediaPlayer 对象。

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

【讨论】:

  • 我遇到的问题是我使用 VideoView,但看不到如何访问媒体播放器。 (我会更新我的代码)
  • 请查看编辑后的答案,让我知道这是否适合您。
  • 好吧,我实际上切换到了表面视图和媒体播放器,它解决了我的问题
  • @Jason... 那么现在您使用媒体播放器了吗?是内联吗?你能发布一些代码吗?我一直在使用列表中的视频视图执行此操作,但滚动会产生一些渲染问题。谢谢。
  • 上面的代码对我不起作用——也许 setLooping 对流视频没有影响?
【解决方案2】:

您可以参考以下代码,其中 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));

【讨论】:

    猜你喜欢
    • 2020-08-20
    • 2012-11-28
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    相关资源
    最近更新 更多