Note that there are some explanatory texts on larger screens.

plurals
  1. POPlay music once CountDownTimer android
    text
    copied!<p>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 <code>onTick</code> rounds to one. Yes, I know that <a href="https://stackoverflow.com/a/6811744/1878685">CountDownTimer is not precise</a> . Thanks for help!</p> <pre><code> 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); } </code></pre> <p>if I use <code>int seconds = Math.round(millisUntilFinished / 1000);</code></p> <pre><code>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 </code></pre> <p>if I use <code>int seconds = Math.round(millisUntilFinished / 1000f);</code></p> <pre><code>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 </code></pre> <p>It`s user set time on the tamer:</p> <pre><code>protected int tTime = 0; public void onClick(View v) { if(v == upTimesl &amp;&amp; tTime &lt;= (11*60*60+1*59*60+55)) settTime(tTime + 5); else if(v == downTimesl &amp;&amp; tTime &gt; 5) settTime(tTime - 5); else if(v == downTimesl &amp;&amp; tTime &lt;= 5) settTime(tTime=0); ... </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload