2015-08-14 55 views
0

我试图仅在rtsp视频流可用时才使用VideoView显示视频。我看到很多人在那里使用按钮来启动VideoView,但没有关于如何监听传入流的示例。使用rtsp的Android VideoView仅在视频流可用时启动

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

如果我尝试之前还有一个流来执行,我得到“无法播放此视频”。如果我事先启动流,它的功能正常。

+0

大概你必须找到一些RTSP客户端代码,你可以直接使用它来确定流是否可用。然后,当它开始使用'VideoView'时。 – CommonsWare

回答

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

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