0
votes

So I have an IP camera that outputs a video stream I can connect to via the rtsp protocol. I want to display this in my android application, so I've set up a videoview using the following code;

VideoView vv = (VideoView)this.findViewById(R.id.videoView);
    Uri uri = Uri.parse("rtsp://username:[email protected]:554/1/stream3");
    vv.setVideoURI(uri);
    vv.requestFocus();
    vv.start();

I'm putting this in the onCreate() of the main activity class, so when the app loads up it automatically connects and starts streaming. My experience with this is that it works - but eventually gets choppy and or just stops randomnly and doesn't seem to ever get back to running again. I have to close the app and clear it from memory and restart it to get it back - but then it loses connection shortly after, meaning its pretty much useless.

I also found it seemed to lag a bit when touching on the screen objects like menus or buttons but that might just be a coincidence - I can't say for sure.

The thing is the stream is perfect from a PC on the same network via VLC using the same URL. So what am I doing wrong, and is there any better method of handling streaming video? I ultimately wanted to mate the videoview with some overlaid text and buttons, and potentially take screenshots when necessary. At the moment I'm lucky if I get video for a few seconds before it cuts out...

Some additional comments;

I've had some success running it for a longer frame of time - so it's not always bad which makes things difficult to diagnose. But when it stops it stops. Does videoview actively try to reconnect if it has lost a connection? Is there a way of demonstrating this with a progress indicator perhaps - so it doesn't look like it's doing nothing?

1

1 Answers

1
votes

A bit late, but for others with the same problem: try debugging by setting listeners to your VideoView? i.e. onError, onCompletion, etc.

For example:

vv.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.d("VideoViewError", Integer.toString(what)); 
                                   //logs the error you're running into

             //You can also put a switch case here to 
               determine what error it is running into exactly: 
            String errorString = "Media Player Error: ";
            switch (what) {
                case MediaPlayer.MEDIA_ERROR_UNKNOWN: {
                    errorString += "Unspecified media player error. ";
                }
                case MediaPlayer.MEDIA_ERROR_SERVER_DIED: {
                    errorString += "Media server died. ";
                }
            }
            switch (extra) {
                case MediaPlayer.MEDIA_ERROR_IO: {
                    errorString += "File or network related operation error.";
                }
                case MediaPlayer.MEDIA_ERROR_MALFORMED: {
                    errorString += "Bitstream is not conforming to the related coding standard or file spec.";
                }
                case MediaPlayer.MEDIA_ERROR_UNSUPPORTED: {
                    errorString += "Bitstream is conforming to the related coding standard or file spec, but the media framework does not support the feature.";
                }
                case MediaPlayer.MEDIA_ERROR_TIMED_OUT: {
                    errorString += "Media operation timed out.";
                }
            }
            Log.d(TAG, errorString);
                return true;
            }
        });

If the stream is 'ending', you will get an onCompletion

setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
         @Override
         public void onCompletion(MediaPlayer mp) {
                   Log.d("VideoViewError", "Media Player reached end of file");
         }
         }
        );

You can do likewise with setOnInfoListener, which lets you know the status of the video view during playback. (Codes are here: http://developer.android.com/reference/android/media/MediaPlayer.OnInfoListener.html)

Maybe not the answer you're looking for, but will hopefully lead you to the right one!