How to Use Flash MX Sound Objects

This document copyright © 2004 by Kenny Bellew of Cowfly.Com Design, kennybellew@hotmail.com
How to Display a Timer in Minutes and Seconds Using Sound Objects Return to Index Do you want all FLA's used for this tutorial? Do you have a question?
Fig. 25: Example of Audio Timer displaying in minutes and seconds.

Special thanks to the band, gunderson, for allowing the use of of their song Cynical.

To create an audio timer that will track your audio in seconds and minutes, you will access the sound objects position and duration properties. As you will recall, the position and duration properties report back in milliseconds. Therefore, to find the total number of seconds, it is as simple as dividing the sound's duration by 1000, and dividing the number of seconds by 60 to obtain the number of minutes. The main code you need to accomplish this is as follows:

musicDuration = _root.musicHolder.myMusic.duration/1000;
//
minutesTotal = Math.floor(musicDuration / 60);
secondsTotal = Math.floor(musicDuration-minutesTotal*60);
seconds = Math.floor((_root.musicHolder.myMusic.position/1000)%60);
minutes = Math.floor((_root.musicHolder.myMusic.position/1000)/60);
hours = Math.floor((_root.musicHolder.myMusic.myMusic.position/1000)/120);
//
_root.timeText01 = minutes + " : " + seconds;
_root.timeText02 = "/ " + minutesTotal + " : " + secondsTotal;

The following is the complete code used to create the player in fig 25.

Place the following code on a key frame that loops continuously. This code preloads a sound object that was exported as an SWF as an event sound (using the attachSound method). It then loads the SWF sound object into an empty movie clip called musicHolder. When the play button is pressed, this code tracks the time represented by seconds and minutes.

this.onLoad = function () {
_root.musicHolder.loadMovie("Cynical.swf");
// sound object called myMusic is embedded in Cynical.swf.
mySoundLoading=0;
_root.loadBar._xscale=mySoundLoading;
}//END onLoad
//
this.onEnterFrame = function () {
mySoundBytesTotal =_root.musicHolder.getBytesTotal();
mySoundBytesLoaded=_root.musicHolder.getBytesLoaded();
//
if (mySoundBytesLoaded>0) {
mySoundLoading=Math.round((mySoundBytesLoaded/mySoundBytesTotal)*100);
_root.loadBar._xscale=mySoundLoading;
if (mySoundLoading<99) {
_root.timeText01= mySoundBytesLoaded + " %";
}

}
//Display timer in Minutes and Seconds
if (mySoundLoading==100) {
musicDuration = _root.musicHolder.myMusic.duration/1000;
//
minutesTotal = Math.floor(musicDuration / 60);
secondsTotal = Math.floor(musicDuration-minutesTotal*60);
seconds = Math.floor((_root.musicHolder.myMusic.position/1000)%60);
minutes = Math.floor((_root.musicHolder.myMusic.position/1000)/60);
hours = Math.floor((_root.musicHolder.myMusic.myMusic.position/1000)/120);
//
_root.timeText01 = minutes + " : " + seconds;
_root.timeText02 = "/ " + minutesTotal + " : " + secondsTotal;
}
}//END onEnterFrame


Previous Next
How to Create Previous and Next Buttons for Sound Objects How to Create a Drag Bar that Affects Sound Position
Return to Index Do you want all FLA's used for this tutorial? Do you have a question?

This document copyright © 2004 by Kenny Bellew of Cowfly.Com Design, kennybellew@hotmail.com