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, 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; } } 

交替

如果我做:

 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; } } 

我得到:

 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 

Solutions Collecting From Web of "IllegalStateException for MediaPlayer.prepareAsync"

mp = MediaPlayer.create(...); 已经准备好返回的MediaPlayer,所以你不能再次调用prepare (或其变体)(也不需要onPreparedListener)。

“在状态8中调用prepareAsync”意味着Mediaplayer已经准备好了。

你打电话给mp.prepare(); 在你的代码?

您更新的问题:

  1. 检查你的AndroidManifest.xml是否有INTERNET权限
  2. 检查您是否启用了一些数据连接,因为您想要从互联网进行stream式传输
  3. “这个解决scheme也失败了”是什么意思? 它是否会抛出IllegalStateException? 从我所看到的情况来看,它根本不会做任何事情,因为在MediaPlayer对象自己准备好之后注册了OnPreparedListener,导致永远不会调用onPrepared()方法。

更好的方法是写:

 MediaPlayer mp = new MediaPlayer(); mp.setDataSource("http://.../movie.mp4"); mp.setOnPreparedListener(this); mp.prepareAsync(); 

我使用下面的代码来播放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; } } 

它显示warnimg W/MediaPlayer: Couldn't open https://something.com/test.mp3: java.io.FileNotFoundException: No content provider: https://something.com/test.mp3 //something.com/test.mp3但工作正常。