2

我想在同一个活动中准备两个 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

非常感谢您的帮助!

4

2 回答 2

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();
于 2013-05-23T15:44:01.743 回答
1

两个媒体播放器的名称相同。这里发生的事情是您开始播放,因此您的 MediaPlayer 播放器处于 .start() 状态,当您获得另一个 MediaPlayer 时,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

于 2013-05-23T04:05:37.407 回答