Playing a Sound

I l @ ve RuBoard

Unfortunately, there is no simple way to play a sound. You will have to import the sound, write several lines of code, and then write several more lines of code to play the sound in anything but the standard way.

Linking and Playing Sounds

The first thing you need to do before playing a sound with ActionScript is to link it. Import the sound into the Library, select Linkage from the Library panel's menu, and set the sound to export with the movie. You can also assign a different linkage name to the sound, but Flash starts you off with the same name as the Library element, which is the same name as the file.


If you are working with a movie that has a lot of sound, it is a good idea to set the default sound compression to Raw in the Publish Settings. When you test your movie, Flash will not take the time to compress the sound. This way, you can test your movie as quickly as possible. Then, change the default sound compression setting to what you really want before publishing a copy for the Web.

For instance, if you import the file mysound.wav, it will appear in the Library as mysound.wav . When you set its Linkage properties so that it exports with the file, it will be assigned the linkage name mysound.wav , but you can change that to anything you want. It is this linkage name that you will use to refer to the sound in your code.

To play a sound, you have to perform at least three steps. The first step is to create a new sound object:

 mySound = new Sound(); 

Then, you need to attach the sound in the Library to this sound object:


Finally, you have to tell the sound object to play the sound:


Here is a simple button handler that plays a sound from the Library:

 on (release) {     mySound = new Sound();     mySound.attachSound("poof.wav");     mySound.start(); } 

You can find this simple example in the movie 22playsound.fla. You can find a slightly more sophisticated approach in the movie 22playsoundfunction.fla. In this movie, a function called playSound is placed in the main timeline. This function includes all the code you need to play a simple sound.

 function playSound(soundName,balance) {     var mySound = new Sound();     mySound.attachSound(soundName);     mySound.start(); } 

By using this function, you can simplify your ActionScript so that you can play a sound with only one line. Here is a button script that uses this function:

 on (release) {     playSound("poof.wav"); } 

The start Command

The start command in the previous example can be used in a few different ways. You can add up to two additional parameters to it to alter the way the sound plays.

The first parameter you can add is an offset. This starts the sound somewhere in the middle, rather than at the beginning. For instance, this line starts the sound off 1000 milliseconds , or 1 second, into the sound:


The second parameter the start command can take is the number of loops . For instance, if you want to start the sound off at 1 second and loop it three times, you would do this:


If you want to start the sound at the beginning and loop it three times, just use 0 as the offset:


The companion command to start is stop . If you are playing a long sound, you can issue a stop command at any time to halt the sound in its tracks. You must supply the sound name again as a parameter. Otherwise, the stop command stops all sounds playing.


Adjusting Volume

You can alter a sound both before and during playback with a variety of commands. All these commands somehow alter the volume of the sound, in both speakers together or separately.

The setVolume command is the simplest of these adjustments. By setting the volume to a number from 0 to 100, you can raise, lower, or silence a sound:


The example movie 22playsoundvolume.fla features a Play button and four different volume buttons . The top one sets the volume to 0, the next one to 10, the next to 50, and the bottom button to 100. The Play button plays a sound 100 times so that you can have the chance to adjust the volume while it plays.

You can click a button during playback to adjust the volume. Note that adjusting the volume of a specific sound does not affect the volumes of other sounds being played at the same time. This means that you can have separate controls for different sounds ”such as background music and instance sounds.

Sound Properties

Sound objects have two properties that you should know about. The first is duration . This is the how long the sound is, in milliseconds.

The sibling property to this is position . This is the location of the current bit of sound being played, in milliseconds.

For instance, if a sound has a duration of 3000, it is 3 seconds long. If the position of the sound is 1500, the sound is right in the middle of being played.

The example movie 22tracksound.fla demonstrates using position and duration to show the playback of a sound visually.

After starting a sound, a movie clip script positions a movie clip named mark at a position along the width of a movie clip named bar.

 onClipEvent(enterFrame) {     // how far along is the sound (0.0 to 1.0)     percentPlayed = thisSound.position/thisSound.duration;     // how wide is the bar     barWidth =;     // set the mark     _root.mark._x = + barWidth*percentPlayed; } 

When a Sound Ends

You can use the duration and position of a sound to determine when a sound ends. They will be equal to each other. However, if a sound loops, the duration and position of a sound will be equal to each other for a moment at the end of each playback of the sound.

A better way to determine the end of a sound is to use the onSoundComplete listener.

This is a function triggered when the sound is finished playing.

 mySound = new Sound(); mySound.attachSound("mySound.aif"); mySound.onSoundComplete = function () {     trace("sound done!"); } mySound.start(); 

Setting the Balance

You can also direct the sound more to one speaker than the other with the setPan command. This is similar to the balance control on a stereo.

You can set the pan of a sound to a value from -100 to 100. If you set it to -100, all sound comes from the left speaker. Setting it to 100 means that all sound comes from the right speaker.


The movie 22monopan.fla has a single-channel (mono) sound in it. When you click the button on the left, the sound plays completely from the left speaker. When you click the button on the right, the sound plays completely from the right speaker.

This is done by setting to the pan to -100 or 100 before playing the sound.

I l @ ve RuBoard

Sams Teach Yourself Flash MX ActionScript in 24 Hours
Sams Teach Yourself Flash MX ActionScript in 24 Hours
ISBN: 0672323850
EAN: 2147483647
Year: 2002
Pages: 272 © 2008-2017.
If you may any questions please contact us: