我想在一秒钟播放声音文件一次,但如果我设置countDownInterval从100到700,我得到操作两次或三次(由于四舍五入)。如果我设置countDownInterval从700到1000,我得到一个范围从10到2的操作,但是如果我设置在1秒内播放一个声音文件,我得到两个播放,因为onTick
四舍五入到一。 是的,我知道CountDownTimer is not precise。 感谢您的帮助!播放音乐一次CountDownTimer android
public void startTimer() {
tCountDownTimer = new CountDownTimer(tTime * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished/1000);
int minutes = seconds/60;
int hours = minutes/60;
minutes = minutes % 60;
seconds = seconds % 60;
String curTime = hours + ":" + minutes + "::" + seconds;
Log.v("log_tag", "Log is here Time is now" + curTime);
tTimeLabel.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
runSec (seconds);
runMin (minutes);
runHou (hours);
if (seconds == 3) {
playAlertSound(R.raw.beep1);
}
else if(seconds == 2){
playAlertSound(R.raw.beep1);
}
else if(seconds == 1){
playAlertSound(R.raw.beep1);
}
else if(seconds == 0){
playAlertSound(R.raw.beep2);
}
如果我使用int seconds = Math.round(millisUntilFinished/1000);
12-06 19:16:54.320: V/log_tag(1121): Log is here Time is now0:0::4
12-06 19:16:55.379: V/log_tag(1121): Log is here Time is now0:0::3
12-06 19:16:56.437: V/log_tag(1121): Log is here Time is now0:0::2
12-06 19:16:57.478: V/log_tag(1121): Log is here Time is now0:0::1
如果我使用int seconds = Math.round(millisUntilFinished/1000f);
12-06 19:20:14.851: V/log_tag(1167): Log is here Time is now0:0::5
12-06 19:20:15.885: V/log_tag(1167): Log is here Time is now0:0::4
12-06 19:20:16.931: V/log_tag(1167): Log is here Time is now0:0::3
12-06 19:20:17.973: V/log_tag(1167): Log is here Time is now0:0::2
它`用户设定的时间在驯:
protected int tTime = 0;
public void onClick(View v) {
if(v == upTimesl && tTime <= (11*60*60+1*59*60+55))
settTime(tTime + 5);
else if(v == downTimesl && tTime > 5)
settTime(tTime - 5);
else if(v == downTimesl && tTime <= 5)
settTime(tTime=0);
...
好的,'tTime'设置为什么? – Sam
我看到CountDownTimer显示“5,4,3,2”并跳过“1”。这是我在[android CountDownTimer - 滴答之间的附加毫秒延迟]中修复的错误之一(http://stackoverflow.com/q/12762272/1267661)。 '1000f'的时间是正确的,因为你从5开始倒数。 – Sam