0

数秒に 1 回だけサウンド ファイルを再生したいのですが、countDownInterval を 100 から 700 に設定すると、(丸めのため) 2 回または 3 回の操作が行われます。countDownInterval を 700 から 1000 に設定すると、10 から 2 の範囲で 1 つの操作が得られますが、サウンド ファイルを 1 秒で再生するように設定すると、1 にonTick丸められるため、2 つの再生が得られます。はい、CountDownTimer が正確ではないことはわかっています。手伝ってくれてありがとう!

  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);
...
4

1 に答える 1

0

数学的には、丸めではなく、床演算を行っています。millisUntilFinished / 1000が実際に 0.9999 の場合、0 になることが保証されます。以下を使用する必要がありますMath.round()

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

( で割っていることに注意してください1000f。 long を整数で割ることは、まだフロア演算です。)

現在の間隔は 100 ミリ秒です。すべての計算は秒に基づいているため、これは意味がありません。以下を使用する必要があります。

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

また、CountDownTimer にはいくつかの癖があります。各間隔に数ミリ秒が追加され、多くの場合、 を呼び出す前に最後の間隔がスキップされますonFinish()Android CountDownTimerでこれらのエラーを削除するために、しばらく前にこのクラスにいくつかの変更を加えました-ティック間の追加のミリ秒の遅延。

于 2012-12-06T18:13:46.513 に答える