2012-02-12 116 views
0

我真的很希望你能幫助我。下面是我的代碼的一部分,並行成功地運行兩個倒計時定時器...我想要做的就是當倒計時結束時播放一個短的MP3文件....我已經嘗試了很多不同的代碼位,但我我在努力做什麼工作......一個快速取勝將是一件好事..倒計時播放鬧鐘

所以圍捕兩個定時器每個需要播放聲音完成後..提前

//Declare Start/Stop button 
Button btnstart = (Button)findViewById(R.id.btnstart); 
Button Button1 = (Button)findViewById(R.id.Button01); 

final TextView mCounter1TextField=(TextView)findViewById(R.id.counter1); 
final TextView mCounter2TextField=(TextView)findViewById(R.id.counter2); 

//Counter 1 
final CountDownTimer Counter1 = new CountDownTimer(9000000 , 1000) { 
public void onTick(long millisUntilFinished) { 
    mCounter1TextField.setText(" " + formatTime(millisUntilFinished)); 
} 

public void onFinish() { 
    start(); 
} 

}; 

//Counter 2 
final CountDownTimer counter2 = new CountDownTimer(9000000 , 1000) { 
public void onTick(long millisUntilFinished) { 
    mCounter2TextField.setText(" " + formatTime(millisUntilFinished)); 

} 

public void onFinish() { 
    start(); 
} 


}; 

//Start Button1 
btnstart.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 
    Counter1.start(); 

    } 
}); 

//Start Button2 
Button1.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 
counter2.start(); 

感謝

Dj

回答

2

我假設start()是你的功能打電話來播放聲音吧?

所以start()的定義中,把下面的代碼:

MediaPlayer mp = MediaPlayer.create(getBaseContext(), sound); //replace 'sound' by your music/sound 
mp.start(); 

希望這有助於!

編輯:想成爲超清晰:)

某處在你的代碼,它寫的是:

public void onFinish() { 
    start(); 
} 

這個方法/函數被調用計數器完成時。 這個函數裏面寫着'start()'

我不知道這個start()是幹什麼的。

在這兩種情況下,我建議你把它(如果它不產生錯誤),和start()後,添加playSound()兩個onFinish()方法裏面。

,然後寫這個功能之外,以下內容:

public void playSound() { 

MediaPlayer mp = MediaPlayer.create(getBaseContext(), sound); //replace 'sound' by your music/sound 
mp.start(); 

} 
+0

開始的定義中()?可以擴大一點我很新的編碼:)謝謝 – Djorn 2012-02-12 12:35:28

+0

我編輯和擴大我的答案很多給你:P閱讀它,如果你仍然不明白,我很樂意向你解釋更多,只是告訴我什麼你不明白 – Karim 2012-02-12 12:47:39

+0

老兄非常感謝 – Djorn 2012-02-12 12:57:06