2014-04-01 27 views
0

試圖將「暫停」按鈕放入播放幾個聲音片段循環的應用程序中。mp.pause();正在崩潰應用程序

當我打電話給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點。所以downvoting它說,這是缺乏研究是一種不公平..

+0

您是否嘗試過加入一樣,如果(mp.isPlaying)mp.pause支票()。這是一個很好的安全性,因爲不管怎樣都不應該調用pause()。 – Broatian

+0

@Broatian本來我有「其他如果(theText.equals(stopplaying)&& mp.isPlaying())」但它沒有任何區別 – Slugshead

+0

你的程序邏輯看起來有缺陷,例如,如果你輸入這個代碼與stopplaying作爲你的參數,那麼它看起來像你會重置()和釋放()mediaplayer實例,然後,*而不創建一個新的*嘗試暫停()開始()和setLooping()它。但是在你調用release()之後,那個實例是無效的並且不能被使用。還要注意,它不是直接觸發崩潰的pause(),而是setLooping()。 –

回答

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); 
     } 
} 
+0

你我的朋友,是什麼傳說是由!這完美無瑕地工作,我現在可以確切地看到我現在出錯了,所以我不能夠感謝你! – Slugshead

+0

很高興幫助。爲了記錄,我不確定它是否強健。你可能會想辦法更​​好地將「將創建一個新的並啓動它」與「將修改現有的」相比「不應該發生」的案例更合適。 –

+0

可以理解的是,總是有改善一切的空間,但只要能夠播放這些音效,並且實際上只有一個按鈕就可以阻止聲音,這正是我今天晚上努力工作的。就目前而言,它的工作原理:)如果還有更多問題出現,我會從那裏看它。再一次,謝謝你.. – Slugshead