2014-02-21 75 views
0

我試圖以5秒的時間間隔一個接一個地運行音頻。但是我不真的看到這種情況發生。我的第三個音頻在列表中被播放,其他人被跳過。這意味着在調試時只有音樂3正在播放,而其他則不是。我會喜歡這樣做的替代方法。此外,我用這種方法做了準備。雖然運行雖然沒有區別。更改正在播放的音頻延遲android

for(int j=0;j<3;j++) 
{ 

    if(j==0) 
     { 
     xnp = MediaPlayer.create(this,R.raw.ticktock); 
     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       try { 
       xnp.prepare(); 
       xnp.start(); 
      } catch (IllegalStateException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     }, 5000); 

     xnp.stop(); 
     } 
    if(j==1) 
     { 
     xnp = MediaPlayer.create(this,R.raw.music2); 
     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       try { 
       xnp.prepare(); 
       xnp.start(); 
      } catch (IllegalStateException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     }, 5000); 

     xnp.stop(); 
     } 
    else if (j==2) 
     { 
     xnp = MediaPlayer.create(this,R.raw.music3); 
     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        xnp.prepare(); 
        xnp.start(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
      }, 5000); 
     xnp.stop(); 
     intenter(); 
     } 

}  

回答

0

您當前的實現不工作,因爲媒體播放是不是在主線程上完成的,所以你會立即播放所有3個媒體資源,所以只有第三個被聽到。

我想你可能想在你的MediaPlayer對象上調用MediaPlayer#setOnCompletionListener。在完成監聽器中,您可以將其延遲放置到您的處理程序上,以排隊播放下一個資源。

+0

你的意思是這樣的: \t對(INT J = 0;Ĵ<3; J ++) \t { \t \t如果(j == 0) \t \t \t { \t \t \t XNP = MediaPlayer的。創建(在此,R.raw.ticktock); \t \t \t xnp.setOnCompletionListener(新OnCompletionListener(){ \t \t \t \t公共無效onCompletion(最終的MediaPlayer MP){ \t \t \t新處理程序()。postDelayed(新的Runnable(){ \t \t \t \t @覆蓋 \t \t \t \t公共無效的run(){ \t \t \t \t \t \t \t \t \t \t \t xnp.start(); \t \t \t \t \t \t \t \t \t} \t \t \t \t},5000); \t \t \t xnp.release(); \t \t \t intenter(); \t \t \t \t} \t \t \t \t}); \t \t \t} – wisejoy

+0

雖然沒有工作!這次我什麼也聽不到! – wisejoy