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. :)
Asked
Active
Viewed 2.1k times
43
-
hmm yes.But I don't know which "catch" will I use. Is there a code that works like VideoView.isPlayable or something that works like that? Thanks for the response :) – emmandroid Jul 19 '11 at 03:48
-
try-catch doesn't work for this case since this is an asynchronous process. You should try setting an onErrorListener. (from the doc. of onErrorListener: "Interface definition of a callback to be invoked when there has been an error during an asynchronous operation (other errors will throw exceptions at method call time).") – Taner Dec 13 '14 at 13:11
4 Answers
65
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.

Femi
- 64,273
- 8
- 118
- 148
-
Thanks for this info. I will this first and notify you if it works. Thanks :) – emmandroid Jul 19 '11 at 04:14
-
-
1@user1810737 I think you figured this out in the meantime, but I guess you must return true in OnError to tell Android that you have treated the error yourself. – Oliver Hausler Dec 11 '14 at 20:35
-
@user1810737 you can check my answer. i have solved it using snippet of code – Naitik Mar 22 '16 at 05:29
8
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;
}
};

ruxy
- 282
- 3
- 9
6
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;
}
});

Naitik
- 796
- 11
- 32
4
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;
}
});

wired00
- 13,930
- 7
- 70
- 73