您好,这里有一段代码……想法是它运行 20 秒,然后在 0 之前的 5 秒播放一个声音,然后在倒计时结束时播放下一个……但它只是播放结束并跳过 5 秒标记...我试过玩它但无法让它工作。
有什么想法吗?
//Counter 2
final CountDownTimer counter2 = new CountDownTimer(20000 , 1000) {
public void onTick(long millisUntilFinished) {
mCounter2TextField.setText(" " + formatTime(millisUntilFinished));
long timeLeft = millisUntilFinished / 1000;
if (timeLeft == 5000)
playAlertSound(R.drawable.sound1);
}
public void onFinish() {
start();
playSound();
}
public void playSound() {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), (R.drawable.sound2));
mp.start();
}
};
最佳答案
这是因为你的:
playSound(...);
与您的计时器在同一个“线程”中。你应该做类似的事情:
if (timeLeft == 5000)
new Runnable(){
void Run(){
playAlertSound(R.drawable.sound1);
}
public void playSound() {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), (R.drawable.sound2));
mp.start();
}
}.run();
或者类似的东西 :D
因为你不能同时减少你的定时器和播放声音! :)
关于android - 在倒计时结束前播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9318103/