Accessibility

Tutorials

Book info

Macromedia Flash 8 Advanced for Windows and Macintosh:
Visual QuickPro Guide

by Russell Chun and H. Paul Robertson

www.peachpit.com

book cover

Resources

Excerpted from “Macromedia Flash 8 Advanced for Windows and Macintosh: Visual QuickPro Guide”
by Russell Chun and H. Paul Robertson
© 2006. Used with the permission of Peachpit. To purchase this book, please visit www.peachpit.com.

Email to a friendEmail to a friend

Enhance your Flash movie with sound

Incorporating sound into your Adobe® Flash® movie can enhance the animation and interactivity and add excitement to even the simplest project by engaging more of the user’s senses. You can play background music to establish the mood of your movie, use narration to accompany a story, or give audible feedback to interactions such as button clicks and drag-and-drop actions. Flash supports several audio formats for import, including WAV, AIF, and MP3, which enables you to work with a broad spectrum of sounds. Flash also gives you many options for sound export, from speech-quality compression to high quality MP3 compression, to help keep your Flash file size to a minimum.

This tutorial explores the Sound class—the Flash class that lets you control sound with ActionScript. You should already be familiar with basic sound handling in Flash, such as importing sounds and assigning them to keyframes with the Event, Start, Stop, and Stream Sync options. If you’re unsure about some of these techniques, review the tutorials and the Help files that accompany Flash for additional information. You’ll learn how to use the sound class to play sounds from the Library dynamically without having to assign them to keyframes.

All these methods, properties, and events of the Sound class give you the flexibility and power to integrate sounds into your movies creatively. You can create a slider bar that lets your viewers change the volume, for example, or add sounds to an arcade game that are customized to the game play.

Use the Sound class

Attaching a sound file to a keyframe in the Timeline is an easy way to incorporate sounds into your movie. Two common ways to integrate sound on the Timeline are to use the Event Sync option to play a clicking sound in the Down state of a button symbol and to use the Stream Sync option to synchronize dialogue with an animation. But if you need to control when a sound plays at run time, change its volume and playback through the left and right speakers dynamically, or retrieve information about the number of seconds it has been playing, turn to the Sound class.

The methods of the Sound class control a sound’s playback behavior, and the properties of the Sound class can help you control its timing and gather MP3 file information. You need to instantiate the Sound class by using a constructor function and assigning it to a variable. When a Sound object is named, you’ll be able to use it to play and modify sound files that you associate with it.

To create a global Sound object:

  1. Select the first frame of the main Timeline, and open the Actions panel.
  2. Enter var and then a name for your new Sound object followed by :Sound to indicate the data type. At the end of the line, enter an equals sign.
  3. Choose ActionScript 2.0 Classes > Media > Sound > “new Sound” (Figure 1). The new Sound() constructor function appears in the Script pane. The constructor can optionally take a MovieClip object as a parameter. In that case, the Sound object controls the sounds in that movie clip. By not specifying a movie clip, you create a Sound object that controls all the sounds in the Flash document.
The completed statement

Figure 1: The completed statement in the Script pane creates the Sound object called mySound_sound.

Attach sounds

After your new Sound object is instantiated, you must associate a sound with it. You can load an external MP3 file into your Sound object, or you can attach a sound from the Library. When you have multiple imported sounds in your Library, you have to convey to the Sound object which one to play and control.

You identify sounds in the Library by using the Linkage option. In addition to identifying your sound, setting the Linkage option causes the sound file to be embedded into the SWF file so that it will be available when called by the Sound object. When an imported sound is given a linkage identifier, you attach it to a Sound object using the attachSound() method.

To attach a Library sound to the Sound object:

  1. Continuing with the file you created in the preceding task, import a sound file by choosing File > Import > Import to Library and selecting an audio file. Your selected audio file appears in the Library. You may import these sound formats: AIF (Mac), WAV (Windows), and MP3 (Mac and Win). More formats may be available if QuickTime is installed on your system.
  2. Select the sound symbol in your Library.
  3. From the Options menu, choose Linkage (Figure 2). The Linkage Properties dialog box appears.
Choose the Linkage option

Figure 2: Choose the Linkage option from the Library for each sound you want to attach.

  1. In the Linkage section, select the Export for ActionScript check box. Leave “Export in first frame” selected.
  2. In the Identifier field, enter a name to identify your sound (Figure 3).
guitarsLoopID sound

Figure 3: This sound is called guitarsLoopID and will be included in the exported SWF file.

  1. Click OK. Flash exports the selected sound in the SWF file with the unique identifier so that it’s available to be attached to the Sound object.
  2. Select the first frame of the main Timeline, and open the Actions panel.
  3. On the next line, after the new Sound constructor function, enter the name of your Sound object followed by a period.
  4. Choose ActionScript 2.0 Classes > Media > Sound > Methods > attachSound. Or, from the code hint drop-down menu, choose attachSound.
  5. With your pointer between the parentheses, enter the linkage identifier of your Library sound within quotation marks (Figure 4). Your Library sound is attached to the Sound object. It’s very important that you specify the linkage identifier within quotation marks. This tells Flash that the word is the literal name of the identifier and not an expression that it must evaluate to determine the name of the identifier.
The attachSound() method

Figure 4: The attachSound() method attaches a sound from the Library (“guitarsLoopID”) to the Sound object (mySound_sound).

Play sounds

After you have created a new Sound object and attached a sound to it from the Library, you can play the sound. Use the start() method to play a sound from your Sound object. The start() method takes two parameters: secondsOffset and loops.

The secondsOffset parameter is a number that determines how many seconds into the sound it should begin playing. You can set the sound to start from the beginning or at some later point. If you have a 20-second sound attached to your Sound object, for example, a secondsOffset setting of 10 makes the sound play from the middle. It doesn’t delay the sound for 10 seconds but begins immediately at the 10-second mark.

The loops parameter is a number that determines how many times the sound plays. A loops setting of 2 plays the entire sound two times with no delay in between. If no parameters are defined for the start() method, Flash plays the sound from the beginning and plays one loop.

To play a sound:

  1. Continuing with the file you used in the preceding task, create a button symbol, place an instance of it on the Stage, and give it a name in the Property inspector. You’ll assign actions to this button to start playing your sound.
  2. Select the first frame of the main Timeline, and open the Actions panel.
  3. At the end of the current script, assign an onRelease event handler to your button.
  4. Within the event handler function, enter the name of your Sound object followed by a period.
  5. Choose ActionScript 2.0 Classes > Media > Sound > Methods > start.
  6. With your pointer between the parentheses, enter 0 followed by a comma and then 5 (Figure 5). The secondsOffset parameter is set to 0, and the loops parameter is set to 5.
the Script pane

Figure 5: A portion of the Script pane shows just the onRelease handler for the button called startButton_btn. This start() method call plays the sound that is attached to mySound_sound 5 times.

  1. Test your movie. When your viewer clicks the button, the sound plays from the beginning and loops five times.

Tips

  • Unfortunately, you have no way of telling the start() method to loop a sound indefinitely. Instead, set the loops parameter to a ridiculously high number, such as 99999.
  • The start() method plays the attached sound whenever it’s called, even when the sound is already playing. This situation can produce multiple, overlapping sounds. For example, when the viewer clicks the button created in the preceding task multiple times, the sounds play over one another. To prevent overlaps of this type, insert a stop() method (as outlined in the following task) right before the start() method. This technique ensures that a sound always stops before it plays again.
  • The start() method always plays the current sound attached to the Sound object. This means you can attach more sounds from the Library to the same Sound object, and the start() method plays the most current sound. You can create three buttons and assign the following script:
var:my_sound:Sound = new Sound();
firstButton_btn.onRelease = function() {
my_sound.attachSound
(“Hawaiian”);
};
secondButton_btn.onRelease = function() {
my_sound.attachSound (“Jazz”);
};
startButton_btn.onRelease = function() {
my_sound.start(0, 1);
};

When you click firstButton_btn and then startButton_btn, you’ll hear the Hawaiian sound. When you click secondButton_btn and then startButton_btn, you’ll hear the Jazz sound. If you begin playing the Jazz sound before the Hawaiian sound finishes, you’ll hear overlapping sounds.

To stop a sound:

  1. Continuing with the file you used in the preceding task, place another instance of the button symbol on the Stage, and give it an instance name in the Property inspector.
  2. Select the first frame of the main Timeline, and open the Actions panel.
  3. Assign an onRelease event handler to your new button instance.
  4. Enter the name of your Sound object followed by a period.
  5. Choose ActionScript 2.0 Classes > Media > Sound > Methods > stop
    (Figure 6).
stopButton_btn.

Figure 6: A portion of the Script pane shows just the onRelease handler for the button called stopButton_btn. This stop method call stops playing the sound attached to the mySound_sound Sound object. The stop method has no parameters.

  1. Test your movie. When your viewer clicks this button, any sound attached to the Sound object that is playing stops.

To stop all sounds:

  • Choose Global Functions > Timeline Control > stopAllSounds (Esc + ss). When this action is performed, Flash stops all sounds, whether they are attached to a Sound object or playing from the Timeline.

Modify sounds

When you use the Sound class, Flash gives you full control of its volume and its output through either the left or right speaker, known as pan control. With this level of sound control, you can let your users set the volume to their own preferences, and you can create environments that are more realistic. In a car game, for example, you can vary the volume of the sound of cars as they approach or pass you. Playing with the pan controls, you can embellish the classic Pong game by making the sounds of the ball hitting the paddles and the walls play from the appropriate sides.

The two methods for modifying sounds are setVolume() and setPan(). The method setVolume() takes a number from 0 to 100 as its parameter, representing the percentage of full volume; 100 represents the maximum volume, and 0 is silence. The method setPan() takes a number from –100 to 100. The setting –100 plays the sound completely through the left speaker, 100 plays the sound completely through the right speaker, and 0 plays the sound through both speakers equally.

To set the volume of a sound:

  1. Continuing with the file you used in the preceding task, place another instance of the button symbol on the Stage, and give it an instance name in the Property inspector.
  2. Select the first frame of the main Timeline, and open the Actions panel.
  3. At the end of the current script, assign an onRelease event handler to your new button.
  4. Within the event handler function, enter the name of your Sound object followed by a period.
  5. Choose ActionScript 2.0 Classes > Media > Sound > Methods > setVolume.
  6. With your pointer between the parentheses, enter a number from 0 to 100 (Figure 7).
volUp_btn.

Figure 7: A portion of the Script pane shows the onRelease handler for a button called volUp_btn. This setVolume() method call restores the volume for the Sound object mySound_sound to 100 percent. Also listed in the Script pane is an onRelease handler for a button called volDown_btn. This setVolume() method call reduces the volume for the Sound object mySound_sound to 20 percent.

  1. Test your movie. First, play your sound. When you click the new button, the volume changes according to the volume parameter.

To set the right and left balance of a sound:

  1. Continuing with the file you used in the preceding task, place another instance of the button symbol on the Stage, and give it an instance name in the Property inspector.
  2. Select the first frame of the main Timeline, and open the Actions panel.
  3. At the end of the current script, assign an onRelease event handler to your new button.
  4. Within the curly braces of the event handler, enter the name of your Sound object followed by a period.
  5. Choose ActionScript 2.0 Classes > Media > Sound > Methods > setPan.
  6. With your pointer between the parentheses, enter a number from –100 to 100 (Figure 8). This single number controls the balance between the left and right speakers.
onRelease handlers

Figure 8: A portion of the Script pane shows the onRelease handlers for rightSpeaker_btn, leftSpeaker_btn, and bothSpeakers_btn.

  1. Test your movie. First, play your sound. When you click this button, the left-right balance changes according to the pan parameter.