Game clock going too slowly

Dear Corona community,
Students of mine have programmed a timer to update every 100 milliseconds. Their code appeared to work until we tried adding music to it. We know that the track is 2’ 12" long yet it appeared to finish playing at the 95 millisecond mark. I set a timer on my mobile and it we found that it was taking 2 minutes for the timer to show 95 milliseconds. Below is the code for the timer. We have tested it on the simulator and on a Samsung Galaxy S3. Any help will be appreciated.

[lua]local maxTime = 1200
local time = 0
local timeDisplay = display.newText(string.format("%.1f seconds", time), 190, 20, native.systemFont, 20)

local function countTime()
time = time + 0.1
timeDisplay.text = string.format("%0.1f seconds", time)
end
local bgTrack = audio.loadStream(“CathysSong.mp3”)
audio.play(bgTrack)

timer.performWithDelay(100, countTime, maxTime)[/lua]

[import]uid: 22300 topic_id: 33078 reply_id: 333078[/import]

Hi there,

When you say that the track “appeared to finish playing at the 95 millisecond mark”, and “it was taking 2 minutes for the timer to show 95 milliseconds”, it sounds like you mean the timer is running slow compared to the speed at which it should be running. Is that right?

I tried the exact code above in the Simulator, just replacing CathySong.mp3 with my own mp3 file, and it ran correctly. The timer ran at the right speed, and it stopped at 120 seconds (2 minutes). What happens exactly when you run it in the Simulator? Does the timer run at the wrong speed when you include playing the mp3, but at the right speed if you comment out lines 9 and 10 above?

Is this snippet of code part of a larger program?

  • Andrew [import]uid: 109711 topic_id: 33078 reply_id: 131371[/import]

Thank you for trying it out, Andrew. Yes, the timer display is working slower than real time. I have commented out lines 9 and 10 so the only thing going on is the timer. I loaded the code in the simulator and pressed start on a timer on my mobile (the standard timer in the Clock APP on my Galaxy). The mobile timer had gone a whole minute when the simulator was showing 45.8 seconds.

The snippet is part of a larger program but I am testing it by itself to isolate the problem.

Sincerely,
Ziad [import]uid: 22300 topic_id: 33078 reply_id: 131414[/import]

Hi Ziad,

Ah, I see what you mean now. I didn’t test it that carefully before. I started a stopwatch and the Simulator simultaneously, and when the Simulator reached two minutes, my stopwatch was at 2:11, which means the Simulator was running about 10% too slowly for me (and even more slowly for you).

I think what this shows is that timer.performWithDelay can’t be relied upon to provide precise timing. Instead, what I would suggest is that you monitor [lua]system.getTimer()[/lua] in an enterFrame event, and increment your [lua]time[/lua] variable each time [lua]system.getTimer()[/lua] goes up by the threshold you’re monitoring (in your case, 100 milliseconds).

  • Andrew [import]uid: 109711 topic_id: 33078 reply_id: 131418[/import]

Thanks, Andrew, I will try that.

Ziad [import]uid: 22300 topic_id: 33078 reply_id: 131420[/import]

Hi there,

When you say that the track “appeared to finish playing at the 95 millisecond mark”, and “it was taking 2 minutes for the timer to show 95 milliseconds”, it sounds like you mean the timer is running slow compared to the speed at which it should be running. Is that right?

I tried the exact code above in the Simulator, just replacing CathySong.mp3 with my own mp3 file, and it ran correctly. The timer ran at the right speed, and it stopped at 120 seconds (2 minutes). What happens exactly when you run it in the Simulator? Does the timer run at the wrong speed when you include playing the mp3, but at the right speed if you comment out lines 9 and 10 above?

Is this snippet of code part of a larger program?

  • Andrew [import]uid: 109711 topic_id: 33078 reply_id: 131371[/import]

Thank you for trying it out, Andrew. Yes, the timer display is working slower than real time. I have commented out lines 9 and 10 so the only thing going on is the timer. I loaded the code in the simulator and pressed start on a timer on my mobile (the standard timer in the Clock APP on my Galaxy). The mobile timer had gone a whole minute when the simulator was showing 45.8 seconds.

The snippet is part of a larger program but I am testing it by itself to isolate the problem.

Sincerely,
Ziad [import]uid: 22300 topic_id: 33078 reply_id: 131414[/import]

Hi Ziad,

Ah, I see what you mean now. I didn’t test it that carefully before. I started a stopwatch and the Simulator simultaneously, and when the Simulator reached two minutes, my stopwatch was at 2:11, which means the Simulator was running about 10% too slowly for me (and even more slowly for you).

I think what this shows is that timer.performWithDelay can’t be relied upon to provide precise timing. Instead, what I would suggest is that you monitor [lua]system.getTimer()[/lua] in an enterFrame event, and increment your [lua]time[/lua] variable each time [lua]system.getTimer()[/lua] goes up by the threshold you’re monitoring (in your case, 100 milliseconds).

  • Andrew [import]uid: 109711 topic_id: 33078 reply_id: 131418[/import]

Thanks, Andrew, I will try that.

Ziad [import]uid: 22300 topic_id: 33078 reply_id: 131420[/import]