倒计时结束前播放声音
您好,这里是一段代码...想法是它运行 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();
}
};
Hi here is a snipit of code...the idea is that it runs for 20 secs then at 5 seconds before 0 it plays one sound then at end of countdown plays the next...but it is just playing the end and skipping the 5 sec mark...I have tried playing with it but can't get it to work.
Any ideas?
//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();
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为您的 :
与您的计时器位于同一“线程”中。你应该这样做:
或者类似的事情:D
因为你不能减少计时器并同时播放声音! :)
It's cause your :
Is in the same 'thread' as your Timer. You should just do somthing like :
Or something like that :D
Cause you cant decrease your Timer and play sound at same time ! :)
您将
millisUntilFinished
转换为timeLeft
中的秒,然后告诉它仅在还剩 5000 秒(而不是毫秒)时播放。当您进行更改来解决此问题时,您还应该更改与
timeLeft <= 5
的比较,以便在使用调用
onTick
时它将开始播放还剩 4999 毫秒。当然,如果声音已经开始播放,您需要添加逻辑以不播放声音。You're converting
millisUntilFinished
to seconds intimeLeft
, and then telling it to only play if there are 5000 seconds (not milliseconds) left.As you're making the change to fix that, you should also change the comparison to
timeLeft <= 5
so it will begin playing ifonTick
is called with4999
milliseconds left. You'll, of course, need to add logic to not play the sound if it's already been started playing.