我想在同一个活动中准备两个 MediaPlayer。我从网络缓冲 mp3,因为它需要一些时间,我加快速度的想法是:当第一个样本正在播放时,第二个正在准备。
不幸的是我不能让它工作。这是缓冲的方法:
private void getMusic(MediaPlayer player, String url) {
player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
player.setDataSource(url);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnPreparedListener(this);
player.prepareAsync();
}
这是 onPrepared 方法。
@Override
public void onPrepared(MediaPlayer player) {
preparedEnd=true;
prepareState++;
player.start();
if(prepareState==1)
getMusic(player2, "some_url");
}
我想如果玩家准备好了,我可以再次使用 prepareAsync() 。我尝试了其他一些想法,但没有人帮助。问题在于prepareAsync(),因为当我使用prepare() 时没有任何问题。
那是我的错误:
05-23 01:35:22.848: E/MediaPlayer(29894): prepareAsync called in state 1
05-23 01:35:22.858: D/AndroidRuntime(29894): Shutting down VM
05-23 01:35:22.858: W/dalvikvm(29894): threadid=1: thread exiting with uncaught exception (group=0x40ab0228)
05-23 01:35:22.868: E/AndroidRuntime(29894): FATAL EXCEPTION: main
05-23 01:35:22.868: E/AndroidRuntime(29894): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.crivline.musicGame/com.crivline.musicGame.NewGame}: java.lang.IllegalStateException
05-23 01:35:22.868: E/AndroidRuntime(29894): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
05-23 01:35:22.868: E/AndroidRuntime(29894): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
05-23 01:35:22.868: E/AndroidRuntime(29894): at android.app.ActivityThread.access$600(ActivityThread.java:139)
05-23 01:35:22.868: E/AndroidRuntime(29894): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
05-23 01:35:22.868: E/AndroidRuntime(29894): at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 01:35:22.868: E/AndroidRuntime(29894): at android.os.Looper.loop(Looper.java:156)
05-23 01:35:22.868: E/AndroidRuntime(29894): at android.app.ActivityThread.main(ActivityThread.java:4977)
05-23 01:35:22.868: E/AndroidRuntime(29894): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 01:35:22.868: E/AndroidRuntime(29894): at java.lang.reflect.Method.invoke(Method.java:511)
05-23 01:35:22.868: E/AndroidRuntime(29894): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-23 01:35:22.868: E/AndroidRuntime(29894): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-23 01:35:22.868: E/AndroidRuntime(29894): at dalvik.system.NativeStart.main(Native Method)
05-23 01:35:22.868: E/AndroidRuntime(29894): Caused by: java.lang.IllegalStateException
05-23 01:35:22.868: E/AndroidRuntime(29894): at android.media.MediaPlayer._prepareAsync(Native Method)
05-23 01:35:22.868: E/AndroidRuntime(29894): at android.media.MediaPlayer.prepareAsync(MediaPlayer.java:1357)
05-23 01:35:22.868: E/AndroidRuntime(29894): at com.crivline.musicGame.NewGame.getMusic(NewGame.java:460)
05-23 01:35:22.868: E/AndroidRuntime(29894): at com.crivline.musicGame.NewGame.selectPlayer(NewGame.java:427)
05-23 01:35:22.868: E/AndroidRuntime(29894): at com.crivline.musicGame.NewGame.prepareMediaPlayer(NewGame.java:413)
05-23 01:35:22.868: E/AndroidRuntime(29894): at com.crivline.musicGame.NewGame.getData(NewGame.java:270)
05-23 01:35:22.868: E/AndroidRuntime(29894): at com.crivline.musicGame.NewGame.onCreate(NewGame.java:125)
05-23 01:35:22.868: E/AndroidRuntime(29894): at android.app.Activity.performCreate(Activity.java:4538)
05-23 01:35:22.868: E/AndroidRuntime(29894): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
05-23 01:35:22.868: E/AndroidRuntime(29894): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
05-23 01:35:22.868: E/AndroidRuntime(29894): ... 11 more
非常感谢您的帮助!