Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android mediaPlayer - is there an "isPrepared()" or "getStatus()" method?

Tags:

I'm having issues with Android's MediaPlayer. It seems like it's missing important functionality, mainly a way to get the current status of MediaPlayer or to find out if it's prepared. I know there's the on prepared listener, but the MediaPlayer lasts longer than the Activity, so when the Playing Activity is resumed, it would be nice to get current player state, whether or not it's trying to load media, whether media is loaded, etc. Am I missing something, or do I have to keep track of the player states myself?

Other functionality that would be nice would be onPlayStateChanged() - I currently have to keep track play state manually. Am I doing it wrong?

like image 511
StackOverflowed Avatar asked Aug 09 '12 03:08

StackOverflowed


1 Answers

Hope it's not too late for an answer. The MediaPlayer class has no such thing as an isPrepared() or a getStatus() method, and you have to keep track of its state yourself. However, that's not that difficult.

The MediaPlayer class has a good state diagram that really helps. You should implement your service based on that diagram. Also, if you always control the MediaPlayer object from the same thread, it's easy to keep track of its state, so I recommend you to do that. The prepareAsync() method is the only asynchronous method that you have to take care of, but you could keep a boolean that indicates that the player is being prepared, which would be 'true' from the prepareAsync() call until onPrepared() is called. Anyway, you can always implement onError and catch the IllegalStateException to avoid crashes if you accidentally call any method from a illegal state.

Nonetheless, the media playback guide helped me a lot.

like image 160
Alex Machado Avatar answered Oct 06 '22 23:10

Alex Machado