05-19 11:52:51.622: ERROR/MediaPlayer(1291): prepareAsync called in state 8
05-19 11:52:51.622: WARN/System.err(1291): java.lang.IllegalStateException
"prepareAsync called in state 8" means the Mediaplayer is already prepared.
are you calling mp.prepare();
in your code?
I use below code to play sound files for http.
BackgroundSound mBackgroundSound = new BackgroundSound();
public void onSoundRequested(final Uri uri) {
mBackgroundSound = new BackgroundSound();
mBackgroundSound.execute(new SoundModel(dicId, uri));
}
public class BackgroundSound extends AsyncTask<SoundModel, Void, Void> {
MediaPlayer mediaPlayer;
@Override
protected Void doInBackground(SoundModel... params) {
SoundModel model = params[0];
final Uri uri = model.getUri();
if (uri == null || uri == Uri.EMPTY) return null;
if (mediaPlayer != null) mediaPlayer.stop();
try {
mediaPlayer = MediaPlayer.create(VocabularyActivity.this, uri);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (Exception e) {
// do nothing.
}
if (mediaPlayer == null) return null;
mediaPlayer.setVolume(1.0f, 1.0f);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
});
mediaPlayer.start();
return null;
}
}
It shows warnimg W/MediaPlayer: Couldn't open https://something.com/test.mp3: java.io.FileNotFoundException: No content provider: https://something.com/test.mp3
but works fine.
Base Problem lies on calling methods of MediaPlayer
at "not allowed states". State Diagram is shown here. For example, calling start()
method without preparing the media file is not allowed and will throw Exception.
Since MediaPlayer
does not expose getState()
method, you should track states externally. Sample implementation can be found here.
Your updated question:
AndroidManifest.xml
onPrepared()
method never to be called. A better approach would be to write:
MediaPlayer mp = new MediaPlayer();
mp.setDataSource("http://.../movie.mp4");
mp.setOnPreparedListener(this);
mp.prepareAsync();
mp = MediaPlayer.create(...); is already preparing the MediaPlayer returned, so you cannot call prepare
(or its variants) again (and there is no need for onPreparedListener as well).