PDA

View Full Version : Audio display elapsed time


frdonath
08-27-2008, 04:44 PM
Hello, i used the following code to display elapsed time of the song, it is working, but i have a problem - on select song in listbox (without playing it), the paragraph displays this: 0-1:59 ..... Well, once i play the song, it dissapears and the right timing is starting.


current = Audio.GetCurrentPos(CHANNEL_BACKGROUND);
length = Audio.GetLength(CHANNEL_BACKGROUND);
current_minutes = Math.Floor(current / 60);
current_seconds = Math.Floor(current - (current_minutes*60));
length_minutes = Math.Floor(length/60);
length_seconds = Math.Floor(length - (length_minutes*60));

if current_minutes < 10 then
current_minutes = "0" .. current_minutes;
end

if current_seconds < 10 then
current_seconds = "0" .. current_seconds;
end

if length_minutes < 10 then
length_minutes = "0" .. length_minutes;
end

if length_seconds < 10 then
length_seconds = "0" .. length_seconds;
end

sTime = current_minutes .. ":" .. current_seconds ;
Paragraph.SetText("Paragraph2", sTime);


Can anyone please help me with this? I'm just an amateur :)
Thanks a lot

Ulrich
08-27-2008, 05:07 PM
Before doing any math, you should check if no error occurred while retrieving the values for GetCurrentPos and GetLength. In the case of an error, the functions return -1. If you ignore the error and just do the conversions you end up with the display of 0-1:59.

Ulrich

frdonath
08-27-2008, 05:30 PM
Oh, thank you ... it sounds a bit confusing for me though, how can i do that? with a code like Application.GetLastError?

Ulrich
08-27-2008, 06:04 PM
All you have to do is test the value returned before proceeding... Like this:

current = Audio.GetCurrentPos(CHANNEL_BACKGROUND);
length = Audio.GetLength(CHANNEL_BACKGROUND);
if ((current ~= -1 ) and (length ~= -1)) then
-- both values are different to -1 and should be valid, continue processing
current_minutes = Math.Floor(current / 60);
current_seconds = Math.Floor(current - (current_minutes*60));
length_minutes = Math.Floor(length/60);
length_seconds = Math.Floor(length - (length_minutes*60));
-- place the rest of the former code here...
else
-- oops, an error occurred, show a static value instead
Paragraph.SetText("Paragraph2", "00:00");
end

By doing this simple comparison, you'll make sure you won't display nonsense.

Ulrich

frdonath
08-27-2008, 06:14 PM
thank you very much Ulrich

yes, it works now - just a small exception, when i play the song, displays the 0-1:59 for a very short time before showing the right timing.