Macromedia Flash 8 Advanced for Windows and Macintosh:
Visual QuickPro Guide
by Russell Chun and H. Paul Robertson
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.
by Russell Chun and H. Paul Robertson
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.
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.
Figure 1: The completed statement in the Script pane creates the Sound object called mySound_sound.
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.
Figure 2: Choose the Linkage option from the Library for each sound you want to attach.
Figure 3: This sound is called guitarsLoopID and will be included in the exported SWF file.
Figure 4: The attachSound() method attaches a sound from the Library (“guitarsLoopID”) to the Sound object (mySound_sound).
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.
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.
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.
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.
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.
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.
Figure 8: A portion of the Script pane shows the onRelease handlers for rightSpeaker_btn, leftSpeaker_btn, and bothSpeakers_btn.