2013-10-14 103 views
10
public static MediaPlayer mp=null; 
public static void playGeneric(int name, final ImageButton button,final ImageButton pervious,Context context) { 
    button.setEnabled(false); 
button.setClickable(false); 
pervious.setEnabled(false); 
pervious.setClickable(false); 
    try{ 
     if(mp != null && mp.isPlaying()) 
     { 
      mp.stop(); 
      mp.release(); 
      mp = null; 
      mp=MediaPlayer.create(context, name); 
      mp.start(); 
     } 
     else 
     { 
      mp = MediaPlayer.create(context, name); 
      mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
       @Override 
       public void onPrepared(MediaPlayer arg0) { 
        //mp.prepare(); 
        mp.start(); 

       } 

      }); 
     mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 

      @Override 
      public void onCompletion(MediaPlayer mp) { 
       // TODO Auto-generated method stub 
       mp.release(); 
       System.out.println("Object released"); 
       button.setEnabled(true); 
       button.setClickable(true); 
       pervious.setEnabled(true); 
       pervious.setClickable(true); 
      } 
     }); 
     } 

    } catch (IllegalStateException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }catch (SecurityException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     e.getMessage(); 
    } catch (NullPointerException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     e.getMessage(); 
    } 

} 

給我java.lang.IllegalStateException上mp.isplaying()方法。java.lang.IllegalStateException在MediaPlayer.isplaying()方法

我想停止音樂,如果它正在播放並播放另一首歌曲。

的logcat:

10-14 15:12:05.474: E/MediaPlayer(15411): prepareAsync called in state 8 
10-14 15:12:05.474: W/System.err(15411): java.lang.IllegalStateException 
10-14 15:12:05.474: W/System.err(15411): at android.media.MediaPlayer.prepare(Native Method) 
10-14 15:12:05.474: W/System.err(15411): at com.rogerscenter.LearnReadWriteSpell.Utility.Music.playGeneric(Music.java:93) 
10-14 15:12:05.474: W/System.err(15411): at com.rogerscenter.LearnReadWriteSpell.LetterCategory.Letter_Lesson1_activity.onCreate(Letter_Lesson1_activity.java:140) 
10-14 15:12:05.474: W/System.err(15411): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
10-14 15:12:05.474: W/System.err(15411): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586) 
10-14 15:12:05.474: W/System.err(15411): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638) 
10-14 15:12:05.474: W/System.err(15411): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
10-14 15:12:05.474: W/System.err(15411): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928) 
10-14 15:12:05.484: W/System.err(15411): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-14 15:12:05.484: W/System.err(15411): at android.os.Looper.loop(Looper.java:123) 
10-14 15:12:05.484: W/System.err(15411): at android.app.ActivityThread.main(ActivityThread.java:3647) 
10-14 15:12:05.484: W/System.err(15411): at java.lang.reflect.Method.invokeNative(Native Method) 
10-14 15:12:05.484: W/System.err(15411): at java.lang.reflect.Method.invoke(Method.java:507) 
10-14 15:12:05.484: W/System.err(15411): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
10-14 15:12:05.484: W/System.err(15411): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
10-14 15:12:05.484: W/System.err(15411): at dalvik.system.NativeStart.main(Native Method) 
+1

請發佈完整的堆棧跟蹤 –

回答

7

由於Android的文檔表明,如果MP是,如果沒有當時java.lang.IllegalStateException被初始化將被拋出,所以你必須,用來初始化第一或者你有寫

退房的文檔http://developer.android.com/reference/android/media/MediaPlayer.html#isPlaying()

嘗試這樣

mp=MediaPlayer.create(context, name); 

    try { 

    if (mp.isPlaying()) { 
     mp.stop(); 
     mp.release(); 
     mp=MediaPlayer.create(context, name); 
    } 



    mp.start(); 
} catch (Exception e) { 
} 
+1

沒有不工作(: – Sydroid

+0

可以爲您做改變什麼,我都建議後發佈的logcat –

+0

那裏張貼的logcat,我dnot知道堆了流動 – Sydroid

7

嘗試將mp.release()更改爲reset()。這可以幫助你。

+0

reset()沒有給我完美的輸出 – Sydroid

+0

它爲我工作:) –

-1

使用runOnUiThread進行mediaRecorder準備。

private boolean prepareMediaRecorder() { 

    mediaRecorder = new MediaRecorder(); 

    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 

      mediaRecorder.reset(); 
      mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 

      mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB); 

      mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 

      mediaRecorder.setOutputFile(filePath); 

      try { 
       mediaRecorder.prepare(); 
      } catch (IOException e) { 
       mediaRecorder = null; 
       return; 
      } 
      mediaRecorder.start(); 
      recording = true; 
     } 
    }); 

    return true; 
} 
+0

Downvoting,因爲:(1)問題是關於MediaPlayer,而不是MediaRecorder,(2)從問題的代碼上下文中很清楚它在UI線程上運行,否則取決於在UI線程上的其他代碼(例如控制啓用和禁用)將失敗,以及(3)不要求從UI線程使用MediaPlayer或MediaRecorder (它們必須用一個Looper在一個線程上*創建*,然後事件將通過該Looper分派,但一旦創建就可以從其他線程使用)。 – Jules

相關問題