mp.pause();正在崩溃应用程序

时间:2014-04-01 17:52:03

标签: android android-mediaplayer audio

尝试在我的应用程序中放置一个“暂停”按钮,播放一些声音片段循环播放。

当我调用mp.pause();一切都破裂了,我完全迷失了!

这是我正在使用的方法..

    protected void managerOfSound(String theText) {
    if (mp != null) {
        mp.reset();
        mp.release();
    }

        if (theText.equals(campfire))
            mp = MediaPlayer.create(this, R.raw.campfire);
        else if (theText.equals(calmthunder))
            mp = MediaPlayer.create(this, R.raw.calmthunderstorm);
        else if (theText.equals(rainthunder))
            mp = MediaPlayer.create(this, R.raw.rainthunder);
        else if (theText.equals(whalesgulls))
            mp = MediaPlayer.create(this, R.raw.whalesandgulls);
        else if (theText.equals(stopplaying))
            mp.pause();

    mp.start();
    mp.setLooping(true);
}

这是一个logcat(喵喵^ _ ^)

threadid=1: thread exiting with uncaught exception (group=0xb2f6c648)
FATAL EXCEPTION: main
java.lang.IllegalStateException
at android.media.MediaPlayer.setLooping(Native Method)
at com.tags4apps.soothingsounds.MainActivity.managerOfSound(MainActivity.java:83)
at com.tags4apps.soothingsounds.MainActivity$1.onClick(MainActivity.java:34)
android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

我现在对任何建议持开放态度

编辑:什么是downvotes,我是晚上7点我一直在寻找一种解决方法,从下午3点开始。所以低估它说它缺乏研究是不公平的。

1 个答案:

答案 0 :(得分:0)

这可能不是完整的答案,但我会尝试解决当前代码中一个相当明显的问题:

protected void managerOfSound(String theText) {
    if (mp != null) {
        mp.reset();
        mp.release();   //from this point on it is illegal 
                        //to operate on the existing mp object
    }
//if you take one of these branches you will get a new mp instance, and be fine
    if (theText.equals(campfire))
        mp = MediaPlayer.create(this, R.raw.campfire); 
   else if (theText.equals(calmthunder))
        mp = MediaPlayer.create(this, R.raw.calmthunderstorm);
    else if (theText.equals(rainthunder))
        mp = MediaPlayer.create(this, R.raw.rainthunder);
    else if (theText.equals(whalesgulls))
        mp = MediaPlayer.create(this, R.raw.whalesandgulls);
//but if you take this branch, you will be illegally operating on a released instance
    else if (theText.equals(stopplaying))
        mp.pause();

    mp.start();
    mp.setLooping(true);
}

相反,做这样的事情:

protected void managerOfSound(String theText) {
    if (theText.equals(stopplaying)) {
        if (mp != null) mp.pause();

    } else {  //we are not pausing
        if (mp != null) {  //lets release any old one
            mp.reset();
            mp.release();   
        }
        //and then get an appropriate new instance
        if (theText.equals(campfire))
            mp = MediaPlayer.create(this, R.raw.campfire); 
        else if (theText.equals(calmthunder))
             mp = MediaPlayer.create(this, R.raw.calmthunderstorm);
        else if (theText.equals(rainthunder))
             mp = MediaPlayer.create(this, R.raw.rainthunder);
        else if (theText.equals(whalesgulls))
             mp = MediaPlayer.create(this, R.raw.whalesandgulls);
        //Note this will still fail if there is some OTHER possibility
        mp.start();
        mp.setLooping(true);
      } 
}