Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safe to reset a MediaPlayer in preparing state?

Tags:

android

I'm writing an audio player using MediaPlayer that allows the user to skip the actual tune. A skip request might occur at any time, including between a call to MediaPlayer.prepareAsync and the upcoming onPrepared callback. The doc says:

It is important to note that the Preparing state is a transient state, and the behavior of calling any method with side effect while a MediaPlayer object is in the Preparing state is undefined.

Does that include calls to reset, or even to release? Because if it so, then I would either have to wait for the onPrepared callback to reuse the MediaPlayer or allocate a brand new MediaPlayer if I don't want to wait and release the obsolete one on the onPrepared callback, right?

like image 523
Simon Avatar asked Feb 11 '11 17:02

Simon


1 Answers

I also built a stream music player and struggled with the preparing state. The worse part of it was that there were some streams where prepare() hung forever downloading (buffering) data without ever calling onBufferUpdate. Calling release did nothing. So, the way I did it was calling reset() on the stuck MediaPlayer from anotherthread after 15 seconds despite the recommendations in the docs. This caused it to throw an exception and brought it to error state. After catching the exception I called release(). This seems to have solved the problem. I hope this will be useful to someone.

like image 195
Puiu Ioan Avatar answered Oct 20 '22 21:10

Puiu Ioan