Android mediaPlayer - is there an "isPrepared()" or "getStatus()" method?
I have found something which might be helpful for you and others. There is a setOnPreparedListener()
function in MediaPlayer class which makes you able to run a piece of code when the MediaPlayer is prepared.
Also, if you want to be sure your media has been loaded, you can use the getDuration()
function. This code returns the duration in milliseconds, if no duration is available it returns -1. Here below is the code:
yourMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if(yourMediaPlayer.getDuration() != -1){
//your code when when something is loading
}
}
}
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.
I needed to see the status while debugging to troubleshoot a problem, so I just placed this code somewhere after I knew the MediaPlayer had been prepared:
try {
this.audioPlayer.prepare();
} catch (Exception e) {
}
That prints an error to the console like "E/MediaPlayer﹕ prepareAsync called in state 32."
Now if I could just find a place where all the status codes are listed...