I'd like to remove the popup message 'Sorry this video cannot be played' from the videoview in my app. I dont want my app to display any error messages.
Set ErrorListener to your VideoView and return true from there and now no error will be shown.
i.e:
yourVideoView.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
return true;
}
});
Related
Below is the piece of my code for handling the error of my video player. This error callback listener gets triggered for the first time only. After that, it's not capturing the error.
videoPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Toast.makeText(getApplicationContext(),
getResources().getString(R.string.msgPleaseNoConnection),
Toast.LENGTH_SHORT).show();
vVideoBufferLoader.setVisibility(View.INVISIBLE);
return false;
}
});
Note:
I tried returning true from that callback which means I handled the error. But it doesn't solve the problem too.
The goal of the MediaPlayer's OnErrorListener is to signal when an error has occurred, at which point the MediaPlayer object is in an end state.
http://developer.android.com/reference/android/media/MediaPlayer.html
If you are using the MediaPlayer constructor to 'reset' the object elsewhere in the code, you are essentially creating a new MediaPlayer object and saving it over the older one. If this is the case, then you also need to reassign the OnErrorListener.
Here's a short snippet of how I've been using OnErrorListener in my app:
private MediaPlayer.OnErrorListener vidVwErrorListener = new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) { //if there was an error in trying to play the intro video
if (tryLrgClip) { // If the larger-resolution clip failed to play, try playing the backup (lower-resolution) clip.
tryLrgClip = false;
trySmClip = true;
vidVwSplashView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + SPLASH_VIDEOS));
vidVwSplashView.start();
} else { // If that didn't work either, give up on playing a video, and do something else
tryLrgClip = trySmClip = false;
vidVwSplashView.setVisibility(View.GONE);
//Something else
}
return true;
}
};
I hope that helps!
Is there a way to handle the Cannot play this video in android video view programatically.
You have to implement MediaPlayer.OnErrorListener
And provide it to the following VideoView method
public void setOnErrorListener (MediaPlayer.OnErrorListener l)
It may look like this
MediaPlayer.OnErrorListener onErrorListener = new MediaPlayer.OnErrorListener()
{
#Override
public boolean onError(MediaPlayer mp, int what, int extra)
{
Log.e(getPackageName(), String.format("Error(%s%s)", what, extra));
return true;
}
};
mp the MediaPlayer the error pertains to
what the type of error that has occurred: MEDIA_ERROR_UNKNOWN MEDIA_ERROR_SERVER_DIED
extra an extra code, specific to the error. Typically implementation dependent. MEDIA_ERROR_IO MEDIA_ERROR_MALFORMED
MEDIA_ERROR_UNSUPPORTED MEDIA_ERROR_TIMED_OUT
MEDIA_ERROR_UNSUPPORTED this constant represents state you are looking for.
Returns True if the method handled the error, false if it didn't. Returning false, or not having an OnErrorListener at all, will cause
the OnCompletionListener to be called.
Our app plays a set of videos, sometimes we are getting can't play this video alert message.
We are either playing video from sd card or streaming if that video is not yet downloaded. Mostly, error arises while streaming with slow internet connection.
I understood few causes of this error from reading some posts and blogs.
But, now I want to play the next video when the error occurs without showing that error message.
I used the below listener for that ,
video.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.d("video", "setOnErrorListener ");
return false;
}
});
Method got invoked when the error arises, but can't stop showing that alert message.
Is there any way to stop showing that alert message?
Thanks in advance.
Returning false or not having an OnErrorListener at all will cause the OnCompletionListener to be called.
So return true instead of false from the function and no error will be shown, i.e.
video.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.d("video", "setOnErrorListener ");
return true;
}
});
For more info see Android Document
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. :)
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.
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;
}
};
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;
}
});
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;
}
});
I am using MediaPlayer to play a video in my app. The video takes a while to buffer and the videoview is blank for that time.
Is there a way to start the buffering when the user is in the previous screen, so that when he comes to the video playing screen, the video is ready to play?
Thanks
Chris
MediaPlayer lets you register an OnPreparedListener and an OnBufferingUpdateListener.
I've not tried it, but you could try calling MediaPlayer.prepareAsync() while the user is on the previous screen, then call .start() when the user moves to the right screen. Not sure where the media player can live during this process... You might need to do this all within one activity that you dynamically update when the user wants to see the video.
like below:
mp.setOnInfoListener(new OnInfoListener() {
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
loadingDialog.setVisibility(View.VISIBLE);
} else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
loadingDialog.setVisibility(View.GONE);
}
return false;
}
});
mediaPlayer.setOnInfoListener(new OnInfoListener() {
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
if (what == 703) {
// Not documented :(
// See http://stackoverflow.com/a/9622717/435605
isBuffering = true;
} else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
isBuffering = false;
}
return false;
}
});