使用rtsp的Android VideoView仅在视频流可用时启动

时间:2015-08-14 16:54:34

标签: android stream android-videoview rtsp

我正在尝试仅在rtsp视频流可用时使用VideoView显示视频。我看到很多例子,人们使用按钮启动VideoView,但没有关于如何监听传入流的内容。

video_stream.setVideoPath("rtsp://MY_IP/stream");
    video_stream.requestFocus();
    video_stream.start();

如果我尝试在有流之前执行此操作,我就无法播放此视频"。如果我事先启动了流,它就能正常运行。

1 个答案:

答案 0 :(得分:0)

设置一个onErrorListener,设置一个onCompleteListener,像我上面那样启动流。

video_stream.setOnErrorListener(new OnErrorListener() {
        public boolean onError(MediaPlayer mp,int what, int extra) {video_stream.setVideoPath("rtsp://IP/stream");
                                video_stream.requestFocus();
                                video_stream.start();}
    });video_stream.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            video_stream.setVideoPath("rtsp://IP/stream");
            video_stream.requestFocus();
            video_stream.start();

        }
    });video_stream.setVideoPath("rtsp://IP/stream");
    video_stream.requestFocus();
    video_stream.start();

每当我们收到错误时,我们都会尝试重新连接。如果流结束,我们会立即尝试重新连接。