我想在同一个活动中准备两个MediaPlayers。我从网上缓冲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
感谢大家提前帮助!
答案 0 :(得分:1)
两个媒体播放器的名称相同。这里发生的是你开始播放所以你的MediaPlayer播放器处于.start()状态,当你获得另一个MediaPlayer时,媒体播放器获得同名播放器并转到.prepare()状态。 Android MediaPlayer不允许您从头开始直接再次准备。你必须在准备之前先停下来。
以下是解决方案: 创建两个MediaPlayers:
MediaPlayer mp1 = new MediaPlayer();
MediaPlayer mp2 = new MediaPlayer();
mp1.setDataSource(yoururl);
mp2.setDataSource(nexturl);
mp1.preapareAsync();
@Override
public void onPrepared(MediaPlayer mp1) {
mp1.start();
}
mp2.prepare();
mp1.setNextMediaPlayer(mp2);
检查此处的媒体播放器流程图,以查看哪些状态可以从一个移动到另一个 http://developer.android.com/reference/android/media/MediaPlayer.html
答案 1 :(得分:1)
我在@Pesal的帮助下解决了这个问题。正确的代码:
MediaPlayer mp1 = new MediaPlayer();
MediaPlayer mp2 = new MediaPlayer();
try {
mp1.setDataSource("source1");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
mp2.setDataSource("source2");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp1.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp1.prepareAsync();
mp2.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp2.prepareAsync();