IllegalStateException for MediaPlayer.prepareAsync
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
try {
mp = MediaPlayer.create(
Main.this,
开发者_StackOverflow社区 Uri.parse("http://codejanitor.us/good.mp3"));
mp.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
try {
mp.prepareAsync();
} catch (IllegalStateException e) {
e.printStackTrace();
}
} finally {
if (mp != null) {
mp.release();
mp = null;
}
}
ALTERNATELY
If I do:
try {
mp = MediaPlayer.create(
AmazonClipActivity.this,
Uri.parse("http://codejanitor.us/good.mp3"));
mp.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
} finally {
if (mp != null) {
mp.release();
mp = null;
}
}
I get:
05-19 12:22:57.472: DEBUG/MediaPlayer(1635): Couldn't open file on client side, trying server side
05-19 12:22:57.472: INFO/StagefrightPlayer(68): setDataSource('http://codejanitor.us/good.mp3')
05-19 12:22:57.482: INFO/NuHTTPDataSource(68): connect to codejanitor.us:80/good.mp3 @0
05-19 12:23:00.632: INFO/NuCachedSource2(68): ERROR_END_OF_STREAM
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).
"prepareAsync called in state 8" means the Mediaplayer is already prepared.
are you calling mp.prepare();
in your code?
Your updated question:
- Check whether you have INTERNET permission in your
AndroidManifest.xml
- Check whether you have some data connection enabled, as you want to stream from the internet
- What do you mean with "this solution also fails"? Does it throw an IllegalStateException? From what I see, it just won't do anything at all, because you register your OnPreparedListener after the MediaPlayer object has prepared itself, causing the
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();
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.
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.
精彩评论