2

编辑3:啊哈!当 SP“提供未准备好的样本 14”时,MP 崩溃了......

编辑 2:我的活动中还有一个 SoundPool 播放短音效,显然,它以某种方式杀死了 MediaPlayer。就像 MediaPlayer 加载一样,播放大约一秒钟,然后在加载 SP 时死亡。如果我稍后加载 MediaPlayer,当 SP 已经加载时,MP 将开始正常播放,但如果我在 SP 中播放一些声音 FX 就会死掉......

old:我正在尝试播放音乐声音文件,大约 2 分钟长...

    try {
        MediaPlayer mp = MediaPlayer.create(main.this, R.raw.naturesounds );
        mp.prepare();
        mp.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

但它抛出

06-29 16:50:52.359: E/MediaPlayer(3772): prepareAsync called in state 8
06-29 16:50:52.359: W/System.err(3772): java.lang.IllegalStateException
06-29 16:50:52.359: W/System.err(3772):     at android.media.MediaPlayer.prepare(Native Method)
06-29 16:50:52.359: W/System.err(3772):     at engineDemo.com.main.fillWorld(main.java:132)
06-29 16:50:52.359: W/System.err(3772):     at engineDemo.com.main.onCreate(main.java:83)
06-29 16:50:52.359: W/System.err(3772):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-29 16:50:52.359: W/System.err(3772):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-29 16:50:52.359: W/System.err(3772):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-29 16:50:52.359: W/System.err(3772):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-29 16:50:52.359: W/System.err(3772):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-29 16:50:52.359: W/System.err(3772):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 16:50:52.359: W/System.err(3772):     at android.os.Looper.loop(Looper.java:130)
06-29 16:50:52.359: W/System.err(3772):     at android.app.ActivityThread.main(ActivityThread.java:3683)
06-29 16:50:52.359: W/System.err(3772):     at java.lang.reflect.Method.invokeNative(Native Method)
06-29 16:50:52.359: W/System.err(3772):     at java.lang.reflect.Method.invoke(Method.java:507)
06-29 16:50:52.359: W/System.err(3772):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-29 16:50:52.363: W/System.err(3772):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-29 16:50:52.363: W/System.err(3772):     at dalvik.system.NativeStart.main(Native Method)

任何想法可能是错误的或播放背景音乐的最佳方式是什么?

谢谢!

编辑:

像这样编辑

    final MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.naturesounds);
    mediaPlayer.setOnPreparedListener(new OnPreparedListener(){

        public void onPrepared(MediaPlayer arg0) {
            Log.e("ready!","ready!");
            mediaPlayer.start();

        }} );

它确实显示了日志,但仍然没有播放...

4

1 回答 1

10

MediaPlayer 上的开发文档示例:

mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer arg0) {
        mp.start();

    }
});
//Below this line is the example from docs.
/*----------------------------------------------------*/
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
//mediaPlayer.start(); // no need to call prepare(); create() does that for you

您的 MediaPlayer 已经 Prepared() 您不需要在创建后调用它。

于 2012-06-29T14:01:27.153 回答