怎么就millisUntilFinished无法检测准确CountDownTimer间隔? [英] How come millisUntilFinished cannot detect exact CountDownTimer intervals?
问题描述
嘿所有,所以有一个相当小的,但恼人的问题与CountDownTimer。目前我有一个时间间隔设置为1000,我试图来检测留在毫秒数我的
onTick()
方法。所以,我可以使用文本到语音的时候还有20秒结束,还剩下10秒,等等。那么,如果我使用:
//说话的时候离开20秒
如果(millisUntilFinished == 20000){
tts.speak(你还剩20秒。,TextToSpeech.QUEUE_FLUSH,NULL);
}
定时器不能准确地检测出了20000毫秒。
所以,我不得不求助于使用:
//说话的时候离开20秒
如果(millisUntilFinished< 20999){
如果(millisUntilFinished> 19999){
tts.speak(你还剩20秒。,TextToSpeech.QUEUE_FLUSH,NULL);
}
}
这问题是,有时文本到语音将是期间加油两次。有什么方法来检测毫秒正好让我没有使用大于或小于?
有什么方法来检测毫秒正好让我没有使用大于或小于?
当然不是。 Android是不是一个实时操作系统。时间将是近似的,只是由于主应用程序线程被在任何特定时刻可能忙于其他工作。
Hey all, so there is a rather small but annoying problem with the CountDownTimer.. Currently I have an interval set to 1000. And I am trying to detect the amount of milliseconds left in my
onTick()
method. So that I can use Text To Speech when there are 20 seconds left, 10 seconds left, etc. Well if I use:
//Speak when 20 seconds left
if (millisUntilFinished == 20000) {
tts.speak("You have 20 seconds left.", TextToSpeech.QUEUE_FLUSH, null);
}
The timer is not able to detect the 20000 milliseconds exactly.
So I have to resort to using:
//Speak when 20 seconds left
if (millisUntilFinished < 20999) {
if (millisUntilFinished > 19999) {
tts.speak("You have 20 seconds left.", TextToSpeech.QUEUE_FLUSH, null);
}
}
The problem with that is that sometimes the text to speech will come on twice during that interval. Is there any way to detect the milliseconds exactly so I dont have to use greater than or less than?
Is there any way to detect the milliseconds exactly so I dont have to use greater than or less than?
Of course not. Android is not a RTOS. Timing will be approximate, simply due to the main application thread being possibly occupied with other duties at any given moment.
这篇关于怎么就millisUntilFinished无法检测准确CountDownTimer间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!