Skip to content Skip to sidebar Skip to footer

Android: How Get Error Of Media Player And Use It?

I use SurfaceView for video player If in stream not load video , in logcat view error info(701,0) How get info(701,0) and use it ? Sample : if(error == 701){ .... }

Solution 1:

Yes, you could use setOnErrorListener(..) to your VideoView and handle the errors there. Here is an example:

    mVideoView.setOnErrorListener(newMediaPlayer.OnErrorListener() {
                @OverridepublicbooleanonError(MediaPlayer mp, int what, int extra) {

                    switch(what){

                        case MediaPlayer.MEDIA_ERROR_UNKNOWN:
                            // handle MEDIA_ERROR_UNKNOWN, optionally handle extras
                            handleExtras(extra);
                            break;

                        case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
                            // handle MEDIA_ERROR_SERVER_DIED, optionally handle extras
                            handleExtras(extra);
                            break;
                    }

                    returntrue;
                }
            });

...

    privatevoidhandleExtras(int extra){
            switch(extra){
                case MediaPlayer.MEDIA_ERROR_IO:
                    // handle MEDIA_ERROR_IObreak;
                case MediaPlayer.MEDIA_ERROR_MALFORMED:
                    // handle MEDIA_ERROR_MALFORMEDbreak;
                case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
                    // handle MEDIA_ERROR_UNSPECIFIEDbreak;
                case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
                    // handle MEDIA_ERROR_TIMED_OUTbreak;

            }
        }

Edit: 701 is an info and not an error, so to handle info you need to attach an info listener setInfoListener()

https://developer.android.com/reference/android/widget/VideoView.html#setOnInfoListener(android.media.MediaPlayer.OnInfoListener)

and follow the same pattern as the error listener. Here is an example:

mVideoView.setOnInfoListener(newMediaPlayer.OnInfoListener() {
            @OverridepublicbooleanonInfo(MediaPlayer mp, int what, int extra) {

                switch(what){
                    case MediaPlayer.MEDIA_INFO_BUFFERING_START:
                        // handle info 701 here, MEDIA_INFO_BUFFERING_START corresponds to 701break;
                }
                returntrue;
            }
        });

Note that this requires a minimum API of 17. And a reference to what you are looking for:

https://developer.android.com/reference/android/media/MediaPlayer.html#MEDIA_INFO_BUFFERING_START

Hope this was useful.

Post a Comment for "Android: How Get Error Of Media Player And Use It?"