Sound

Adding Background Sound to a Flash Movie

Sound is added only to keyframes - if you attempt to put sound in a frame that is not a keyframe, the sound will attach itself to the last keyframe before the current location of the playhead. Once you place the sound in the movie, you will want to set the sync setting for the sound in the Property Inspector. The possible settings are as follows:

In the Property Inspector, you can also set the Repeat option - repeating Event or Start sounds has no effect on file size, but repeating or looping a Stream sound will increase the file size significantly. In the Property Inspector, you can also click the Edit button which allow you to control the volume of your sound at various points, thus allowing fadein or fadeouts.

Adding Sound to a Movie Using ActionScript

Previously, we added a sound to the movie by placing it in a keyframe. Alternatively, you can add sound to a movie using ActionScript. The steps are shown below:

With the sound in the library, right click the sound and choose Properties. You will then see either a Basic button or an Advanced Button. If you see the Basic button, then the Advanced Properties you will need are already displayed. If you see the Advanced Button, click on the Advanced Button to see the Advanced Properties. With the Advanced Properties displayed, under the Linkage section, check the Export for ActionScript box. Type any name in the Class box (the example below assumes I use SoundTrack) but leave the Base Class unchanged. The class name you type will be derived from the base class and thus inherit the properties, methods, and events of the base class. A warning message will appear indicating that the new class could not be found and just click OK.

Create an actions layer if it does not already exist and then open the Actions Panel (Window>Actions or F9). Tyep the following:

stop();
var sndTrack:SoundTrack = new SoundTrack();
var sndControl:SoundChannel;
sndControl = sndTrack.play(0,999);

The second statement creates a new instance of the derived class SoundTrack and associates it with the variable sndTrack. The third statement creates a variable of type SoundChannel (SoundChannel is a class used to control the starting and stopping of sound). The play method invoked in the fourth statement is the play method of the base class flash.media.Sound which is inherited by the derived class and then invoked by using the standard objectname.methodname() syntax. The 0 value determines how long Flash will wait before the sound starts playing and the 999 determines how many times the sound will loop.

Controlling Sound with On/Off Buttons

Create a buttons layer and place an instance of two buttons on the layer. Name the instances (below we use on_btn and off_btn). In the Actions layer, use the code from above and add an event listener for each of the buttons. Then create the two functions referenced in the eventListeners as below:

function soundOn(event:MouseEvent):void {sndControl = sndTrack.play(0,999);

function soundOff(event:MouseEvent):void {sndControl.stop();}

Thus one never drags the sound into a keyframe as in the "Background Sound" section above, but instead controls everything through ActionScript.

Revised August 3, 2010. Comments to William Pegram, bill@billpegram.com