How to catch “Sorry, This video cannot be played” error on VideoView

社会主义新天地 提交于 2019-12-08 23:35:54

问题


I have a VideoView and I am streaming videos from a remote server. Most of the times It would play the videos very smoothly. But sometimes, it displays an error message "Sorry, This video cannot be played". I have a hunch that this is more on the supported video formats. However, I don't know which are the supported formats. My question is "How can I catch this error (e.g. Prevent the error message from appearing)"? I am using Android 2.2 on this project. Any advice would be greatly appreciated. :)


回答1:


Try using setOnErrorListener: the documentation says If no listener is specified, or if the listener returned false, VideoView will inform the user of any errors., so I'm assuming if you set one and return true it will not show the user error.




回答2:


The code I used for this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    vView = (VideoView) findViewById(R.id.videoView1);

    vSource = "android.resource://com.domain.android/"
            + R.raw.introductionportrait;
    vView.setVideoURI(Uri.parse(vSource));

    vView.setOnErrorListener(mOnErrorListener);
    vView.requestFocus();
    vView.start();
}

private OnErrorListener mOnErrorListener = new OnErrorListener() {

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        // Your code goes here
        return true;
    }
};



回答3:


I prefer setting listeners like this within onCreate method. Hopefully helps someone out

videoView.setOnErrorListener(new OnErrorListener () {
    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        Log.e(TAG, "Error playing video");
        return true;
    }
});



回答4:


you can add code like below, it will close video view screen if any error occurred. Also, it will not display default popup of saying video can't play :)

 videoview.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
                finish();
                return true;
            }
        });


来源:https://stackoverflow.com/questions/6742002/how-to-catch-sorry-this-video-cannot-be-played-error-on-videoview

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