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

后端 未结 4 2024
执念已碎
执念已碎 2021-01-01 08:49

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 \"So

相关标签:
4条回答
  • 2021-01-01 09:30

    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.

    0 讨论(0)
  • 2021-01-01 09:32

    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;
                }
            });
    
    0 讨论(0)
  • 2021-01-01 09:34

    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;
        }
    });
    
    0 讨论(0)
  • 2021-01-01 09:36

    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;
        }
    };
    
    0 讨论(0)
提交回复
热议问题