2012-12-06 96 views
0

我想在一秒鐘播放聲音文件一次,但如果我設置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); 
... 
+0

好的,'tTime'設置爲什麼? – Sam

+0

我看到CountDownTimer顯示「5,4,3,2」並跳過「1」。這是我在[android CountDownTimer - 滴答之間的附加毫秒延遲]中修復的錯誤之一(http://stackoverflow.com/q/12762272/1267661)。 '1000f'的時間是正確的,因爲你從5開始倒數。 – Sam

回答

0

數學你是不是四捨五入,你一個重新進行地板操作。如果millisUntilFinished/1000實際上是0.9999可以保證得到0。您應該使用Math.round()

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

(請注意,我感到1000f分,潛水長一個整數仍是地板動作)

你當前的時間間隔是100ms,這是沒有意義的,因爲所有的計算都是以秒爲單位的。您應該使用:

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

而且CountDownTimer有幾個怪癖:它增加了幾毫秒到每一個間隔,經常打電話onFinish()之前跳過最後一個區間。我在這個班上做了一些改動,回到android CountDownTimer - additional milliseconds delay between ticks以消除這些錯誤。

+0

我同意你的四捨五入和延遲(測試不同的方法)。但如果我使用'int seconds = Math.round(millisUntilFinished/1000f);'計時器停止兩秒鐘。這裏是日誌文件: '12 -06 18:58:10.191:V/log_tag(649):日誌在這裏時間現在是0:0 :: 5 12-06 18:58:11.239:V/log_tag(649 ):日誌在這裏時間現在是0:0 :: 4 12-06 18:58:12.278:V/log_tag(649):日誌在這裏時間現在是0:0 :: 3 12-06 18:58:13.318 :V/log_tag(649):日誌在這裏時間現在是0:0 :: 2' – Roman

+0

將您的LogCat添加到您的問題中(點擊左下角的「編輯」),以便我可以看到更多的LogCat。 – Sam