Search code examples
javaandroidmedia-playerandroid-mediaplayer

Android MediaPlayer Error Feedback


I am using MediaPlayer in my code to play video.

This is the code :

mp.setDataSource(source);
mp.setOnCompletionListener(this);
mp.setOnErrorListener(this);
mp.prepareAsync();

In some cases the video is not playing(and if i call mp.getDuration(); i get fail) and instead of getting OnError feedback i am getting OnCompletion feedback, and i can't know if any problem occurs.

And some times i get OnError function called.

Any idea how i can check if the mediaplayer was fail in the OnCompletion function?


Solution

  • According to the Docs, onErrorListener 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.

    So your onCompletion is being called when onError returns false.

    Your implementation of onErrorListener should return true to avoid onCompletion to be called.