How to properly cancel Android MediaPlayer.prepare()

Ask A Question

Login To post a question

I am using the Android MediaPlayer to play a Video in my app.

I use MediaPlayer.prepare() instead of MediaPlayer.prepareAsync() deliberately, because I want to be able to know when the statement is finished so that I can, for example, hide a progress bar.

I realize that I could do this with a MediaPlayer.prepareAsync(), but since this would create an ugly overhead in my code, would only be used for this detection and probably wouldn’t help in this situation anyway, I don’t want to do that.

When I start my activity with the video loading and I try to go back to the previous activity before it finished MediaPlayer.prepare(), it throws an IllegalStateException.

All the steps I take before that are synchronized, so just catching the Exception and ignoring it would be possible here, however, I don’t want to have throwing and catching an Exception as expected behavior if I can avoid it.

I’ve already tried to call MediaPlayer.reset() while MediaPlayer.prepare() is running, but the same thing happens.

For more info please click here

Have a question or need a custom quote?