Android: How get error of Media Player and use it?

眉间皱痕 提交于 2019-12-24 08:16:04

问题


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){
   ....
}

回答1:


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

    mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(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;
                    }

                    return true;
                }
            });

...

    private void handleExtras(int extra){
            switch(extra){
                case MediaPlayer.MEDIA_ERROR_IO:
                    // handle MEDIA_ERROR_IO
                    break;
                case MediaPlayer.MEDIA_ERROR_MALFORMED:
                    // handle MEDIA_ERROR_MALFORMED
                    break;
                case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
                    // handle MEDIA_ERROR_UNSPECIFIED
                    break;
                case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
                    // handle MEDIA_ERROR_TIMED_OUT
                    break;

            }
        }

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(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {

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

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.



来源:https://stackoverflow.com/questions/38893949/android-how-get-error-of-media-player-and-use-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!