First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Kenny Bellew

How to Control the Position of a Sound Object

Flash MX allows much greater control over the time position of a sound object. Because you can detect when an exact amount of seconds have passed from the beginning of a sound, it is possible to use a sounds position in time to activate events in your Flash movie. This provides an additional powerful tool to control your presentation.

Fading a Sound Object Five Seconds from the End of the Song

Fig. 10: Example of fading a sound object five seconds from the end of the song

To begin a fade out five seconds from the end of a song, the sound object properties "duration" and "position" can be used. The property "duration" reports the duration of a sound in milliseconds. The property "position" reports the number of milliseconds the sound has been playing.

For a sound object defined with the instance name of "myMusic", the syntax for these methods is as follows:


Using the "duration" property, you can define a variable that equals the duration of a sound minus 5 seconds:

myMusicDuration = _root.myMusic.duration/1000;
myMusicPosition = _root.myMusic.position/1000;

In the above example, the variable "myMusicDuration" is defined as equaling the duration of the sound object "myMusic" divided by 1000. The variable is divided by 1000 because the duration is reported in milliseconds (5000 milliseconds equals 5 seconds). Similarly, the variable "myMusicPosition" is defined as equaling the current position of the sound object "myMusic". Finally, the variable "fadeEnd" is defined as "myMusicDuration" minus five. The number five can be used (versus 5000) because "myMusicDuration" has already been divided by 1000.

By placing the above three lines of code in a movie clip that loops (adding it to the first frame) using the event handler, "onEnterFrame", the variables are updated every time the frame is processed. The ActionScript watches for "fadeEnd" to be equal to or less than "songPosition", and then begin fading the volume of the sound object.

this.onEnterFrame = function () {
myMusicDuration = _root.myMusic.duration / 1000;
myMusicPosition = _root.myMusic.position / 1000;
fadeEnd = (myMusicDuration - 5);
//Fade Out
if (fadeEnd <= songPosition && myMusicVolume>0 && playing==true) {
if (myMusicVolume<=1) {myMusicVolume=0}

In order to make this work, the variable "myMusicVolume" must equal some number before the above script runs. If it does not, the first time you indicate that the setVolume is "myMusicVolume", the volume will be zero. There are a few ways to resolve this. One way is to define "myMusicVolume" as a variable at the same time as the sound object is defined.

myMusic = new Sound(myMusicMc);

Experiment with the Flash example in Fig. 10 to see the sound fade in or out.

» Level Intermediate

Added: 2002-08-13
Rating: 8.98 Votes: 664
» Author
Kenny Bellew is a technical writer in Minneapolis, MN. He's currently interested in becoming involved in other writing-related Flash projects.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.