How to Convert Milliseconds to Minutes and Seconds
Someone recently asked in the Adobe Flash ActionScript forum how to convert milliseconds into the minutes-and-seconds format mm:ss. This person wanted to format the value returned by Sound.duration in order to populate a dynamic text field to indicate the progression of a song — certainly a reasonable endeavor. I also think it’s a fun way to demonstrate how to “roll your own” in cases where ActionScript lacks native support for whatever you’re hoping to achieve. I’ll recount here what I posted in reply.
An answer, short and sweet
Start a new FLA and place a dynamic text field into its own layer. Using the Property inspector, give this text field the instance name output. Create a scripts layer and type the following ActionScript into frame 1 (this code assumes you have an MP3 file named audio.mp3 in the same folder as the published SWF).
var music:Sound = new Sound();
music.loadSound("audio.mp3", true);
var minutes:Number = 0;
var seconds:Number = 0;
this.onEnterFrame = function() {
minutes = Math.floor(music.position / 1000 / 60);
seconds = Math.floor(music.position / 1000) % 60;
output.text = minutes + ":" + seconds;
}
How it works
An arbitrarily named variable, music, is declared and set to an instance of the Sound class. The Sound.loadSound() method is invoked on this instance, which causes the external file audio.mp3 to load and begin to play as soon as enough audio data become available. This happens because the second method parameter, responsible for streaming (actually progressive downloading) is set to true. Another two arbitrarily named variables, minutes and seconds, are declared as numbers and initialized to zero.
Next, a function is assigned to the MovieClip.onEnterFrame event of the main timeline (the global property this refers to the main timeline because that’s where this script is written). At a default speed of twelve times a second (configurable via the movie’s framerate), the following occurs:
- The
minutesvariable is set to the value ofSound.position, as referenced against themusicinstance.Sound.positionreturns a value in milliseconds of the current position of the audio (at one second in,Sound.positionreturns 1000). Actually,minutesis set to the milliseconds value divided by 1000, which provides seconds, then divided again by 60, which provides minutes. This value is run throughMath.floor()to round it down to the nearest integer. - The
secondsvariable is set to the value ofSound.positiondivided by 1000 — again, to provide seconds — but in addition, it is evaluated against the modulo operator (%) so that it never goes higher than 59 (see Lesser Known Operators: Modulo (%) for details). This value is also rounded down to the nearest integer. - Finally, the
TextField.textproperty of theoutputinstance is set to a concatenation of minutes, a colon (:), and seconds.
Variation
Usually, when time is displayed, numbers less than ten are shown with a leading zero. In ActionScript, a leading zero refers to an octal number, rather than decimal, so leading zeroes would have be strings in this scenario. The concatenation above takes care that, so you could, for example, use two conditional operators (?:) to compactly provide leading zeroes (see Lesser Known Operators: Conditional (?:) for details).
Update the above output.text line to look like this:
output.text = ((minutes < 10) ? "0" + minutes : minutes) + ":" +
((seconds < 10) ? "0" + seconds : seconds);
Thanks to Brad Maylor for bringing an error in the Variation section to my attention. I have corrected the code.
August 22nd, 2006 at 6:09 am
Great little tutorial, thanks! The lesser known operators solved my headache
August 24th, 2006 at 5:40 pm
Its the little things that make the difference. Thanks
September 26th, 2006 at 1:45 pm
thanks, saved me some much needed brain matter :p
November 7th, 2006 at 12:50 am
Lesser known blogs; may this never be one of them.
Thanks a lot!
November 7th, 2006 at 10:27 am
Thanks, Mat, Paul, stef, and Esteban!
May 2nd, 2007 at 6:12 pm
To take this a step further…you can use this same logic for FLV’s as well.
Just use myFLVPlayer.totalTime and myFLVPlayer.playheadTime. Both are already in seconds, so you have to convert them into milliseconds first, but after that…works the same.
July 11th, 2007 at 11:32 am
great tutorial!
July 18th, 2007 at 9:45 am
black widow,
Thanks!
August 24th, 2007 at 12:12 pm
You seem to have the answers to everything! Thank you!
August 26th, 2007 at 7:42 pm
Denise,
I sure wish I did!
But I’m certainly glad this blog entry was helpful to you.