Android 用于视频完成的侦听器(或处理程序)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1847818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:42:35  来源:igfitidea点击:

Listener (or handler) for video finish

androidmedia-player

提问by Rui Gon?alves

I have implement the following code in order to test playing a video from a remote web server through it′s URL.

我已经实现了以下代码,以便通过它的 URL 测试从远程 Web 服务器播放视频。

videoView = (VideoView)this.findViewById(R.id.videoView);
        MediaController mc = new MediaController(this);
        videoView.setMediaController(mc);
        videoView.setVideoURI(Uri.parse("http://sayedhashimi.com/downloads/android/movie.mp4"));
        videoView.requestFocus();

The code is working just fine, even in the Android emulator. I just would like to know if there's any listener (or handler) to detect the finish of the video that is being reproduced?

代码运行良好,即使在 Android 模拟器中也是如此。我只想知道是否有任何侦听器(或处理程序)来检测正在复制的视频的完成情况?

Update:

更新:

I have the following code now:

我现在有以下代码:

   @Override
        protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.player);

        videoView = (VideoView)this.findViewById(R.id.videoView);
        playVideo();

        // video finish listener
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                // not playVideo
                            // playVideo();

                            mp.start();
            }
        });
    }

    public void playVideo() {
            MediaController mc = new MediaController(this);
            videoView.setMediaController(mc);
            videoView.setVideoURI(Uri.parse("http://sayedhashimi.com/downloads/android/movie.mp4"));
            videoView.requestFocus(); 
        }

When the activity is just called, the video works fine but when the video finishes, I would like that it played again, which it's not occurring.

当活动刚刚被调用时,视频工作正常,但是当视频结束时,我希望它再次播放,但它没有发生。

回答by Anthony Forloney

Seems you are looking for

似乎你正在寻找

setOnCompletionListener(MediaPlayer.OnCompletionListener l)

More in depth explanation can be found here

更深入的解释可以在这里找到

EDIT

编辑

Thisshows a solution where playback is called after completion using VideoView, MediaControllerand setOnCompletionListener().

显示了使用VideoView,MediaController和完成后调用播放的解决方案setOnCompletionListener()

回答by Imon

here is my working chunk of code:

这是我的工作代码块:

mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() 
        {           
            public void onCompletion(MediaPlayer mp) 
            {
                // Do whatever u need to do here

            }           
        });   

回答by Brennon Reid

        vidView=(VideoView)findViewById(R.id.vidView);
    vidView.setMediaController(null);
    vidView.setVideoPath( "/mnt/external_sd/somerandommovie.3gp" );
    vidView.start();

    vidView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
                        vidView.start();
        }
    });

I found that the listener was firing, the issue was mp.start() didn't seem to do anything, so calling start again on the original object seems to be working fine.

我发现监听器正在触发,问题是 mp.start() 似乎没有做任何事情,所以在原始对象上再次调用 start 似乎工作正常。

回答by rahil008

in your onCompletion listener, try using videoView.start(); instead of mp.start();

在您的 onCompletion 侦听器中,尝试使用 videoView.start(); 而不是 mp.start();

回答by n0sferat0k

Make sure you are using the player on the main thread, it seems the callback only works on the main thread.

确保您在主线程上使用播放器,似乎回调仅适用于主线程。