0

I want to play a sound file ONLY once in some second, but if I set countDownInterval of from 100 to 700, I get the operation two or three times (due to rounding). If I set countDownInterval of from 700 to 1000, I get one operation in a range from 10 to 2, but if I set play a sound file in 1 second, I get two play, because onTick rounds to one. Yes, I know that CountDownTimer is not precise . Thanks for help!

  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);
              }

if I use 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

if I use 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

It`s user set time on the tamer:

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);
...
Community
  • 1
  • 1
Roman
  • 1,045
  • 1
  • 12
  • 33
  • Ok, what is `tTime` set to? – Sam Dec 06 '12 at 19:41
  • I see the CountDownTimer displays "5, 4, 3, 2" and skips "1". This is one of the errors I fixed in [android CountDownTimer - additional milliseconds delay between ticks](http://stackoverflow.com/q/12762272/1267661). The time with `1000f` is correct because you are counting down from 5. – Sam Dec 06 '12 at 20:47

1 Answers1

0

Mathematically you are not rounding, you are taking the floor operation. If millisUntilFinished / 1000 is actually 0.9999 you are guaranteed to get 0. You should use Math.round():

int seconds = Math.round(millisUntilFinished / 1000f);       

(Notice I am dividing by 1000f, diving a long by an integer is still a floor operation.)

Your current interval is 100ms, this doesn't make sense since all of your calculations are based off of seconds. You should use:

tCountDownTimer = new CountDownTimer(tTime * 1000, 1000) { 

Also CountDownTimer has a couple quirks: it adds a few milliseconds to each interval and often skips the last interval before calling onFinish(). I made a few changes to this class a while back to remove these errors in android CountDownTimer - additional milliseconds delay between ticks.

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
  • I agree with you on account of rounding and delay (test different approaches). But if I use the 'int seconds = Math.round(millisUntilFinished / 1000f);' timer stops for two seconds. Here is the log file: '12-06 18:58:10.191: V/log_tag(649): Log is here Time is now0:0::5 12-06 18:58:11.239: V/log_tag(649): Log is here Time is now0:0::4 12-06 18:58:12.278: V/log_tag(649): Log is here Time is now0:0::3 12-06 18:58:13.318: V/log_tag(649): Log is here Time is now0:0::2' – Roman Dec 06 '12 at 18:59
  • Add your LogCat to your question (click "edit" in the lower left corner), so I can see more of the LogCat. – Sam Dec 06 '12 at 19:09