Adding Video and Sound - Adding Sounds to Movies
(Page 4 of 9 )
Once you have imported sound into a document's library, placing it into the movie is simple. Click the keyframe where you want the sound to begin playing. Then, in the Property Inspector, select the sound you want in the Sound drop-down menu (Figure 10.2 shows the sound "narration.avi Audio" in both the Library panel and in the Property Inspector). Next, you can choose either Event or Stream in the Sync drop-down menu. An event sound downloads completely before it begins playing and needs only a single frame. A stream sound, on the other hand, uses a progressive download method of playing and will start playing after only a few frames are downloaded, resulting in the sound beginning much sooner. In addition, a stream sound is tied to the Timeline. Whereas an event sound requires only a single frame, a stream sound requires frames for the entire sound. The playhead moves at the speed of the sound, and if the computer cannot draw the animation fast enough to keep up with the sound, Flash will skip frames to make it do so. Table 10.1 shows the differences between the two types of sound.
Table 10.1 Stream and Event Sounds
Sound | Download | Timeline Coordinated | Frames Required |
Event | Complete | No | One |
Stream | Progressive | Yes | The number necessary to encompass the entire sound |
An event sound will play after the movie has stopped, whereas a stream sound stops as soon as the playhead stops. Therefore, long after a movie has reached its last frame or a stop() action, an event sound can keep on playing, even repeatedly.
Besides selecting Event or Stream in the Sync drop-down menu, you can select the Start and Stop options. The Start and Stop options are typically used in pairs. One keyframe is used to start the sound, and another is used to stop the sound. Selecting the Start option without using a Stop option functions like selecting an event sound. You can also add a Stop option in the layer where you have placed your sound with an event or stream sound.
The Repeat/Loop option's primary use is for looping event sounds. Loops simply repeat a sound a specified number of times, and they shouldn't be used with stream sounds. Using loop sounds is the main method employed to load high-quality music to a Flash movie at minimum load time costs. Basically, the idea is to use a single music loop that keeps repeating itself but sounds like a single continuous song. For example, a high-quality sound set at 44KHz that loops 12 times is loaded only once but plays music the length of time a single sound file 12 times its size would play. Thus, instead of having to reduce the quality of the music, the developer simply uses a single, shorter, high-quality music file that repeats itself. The smaller length of the musical piece is where file size is kept small, rather than sacrificing quality. However, this method is only successful when used with loops.
Note: Free Sounds - You can download hundreds of different music loops free from http://www.flashkit.com.
Using the Behaviors Panel to Load Sounds Using the Behaviors panel, you can load and play sounds from external sources or the library. You can stop sounds globally and stop specific sounds as well. Table 10.2 shows the behaviors available for sound.
Table 10.2 Sound Behaviors
Behavior | Action |
Load Sound from Library | Uses a linkage name to load and/or play sound imported to the library |
Load Streaming MP3 File | Uses progressive download to load and/or play an external MP3 file |
Play Sound | Plays sound identified by an instance name |
Stop All Sounds | Stops all sounds on all layers |
Stop Sound | Stops only sound identified by a linkage name and instance name |
Using the Behaviors panel to add sound and to start and stop it is quite simple. Use the following steps to set up and play a sound file for access by the code generated through the Behaviors panel:
Import a sound file to the library.
Select the sound file in the Library panel and, from the Library menu or context menu, select Linkage.
In the Linkage Properties dialog box, check Export for ActionScript and provide an identifier (no spaces) for the sound.
Place a Button component on the Stage and label it Play Sound. With the button selected, click the plus (+) button in the Behaviors panel and then select Sound, Load Sound from Library.
In the Load Sound from Library dialog box, type the linkage name and then type some other name for the code to use as a sound instance reference. (Any name will do.) Check the check box labeled Play This Sound When Loaded. Before clicking the OK button, you might want to jot down the linkage and instance names because you will need them both in step 7.
On a different layer, drag another Button component to the Stage and label it Stop Sound. With the button selected, click the plus (+) button in the Behaviors panel and then select Sound, Stop Sound from Library.
In the Stop Sound dialog box, enter the same linkage name and sound instance name you used in the Load Sound from Library dialog box in step 5.
Now test the movie. When you click the Play Sound button, you should hear the sound, and as soon as you click the Stop Sound button, the sound should stop.
One of the more useful actions to quickly generate with the Behaviors panel is streaming external MP3 files. By using external streaming, you can lessen the size of the SWF file, thereby reducing file size and load time. Because external files that will be streamed in don't add to the file size of the document, you can use any size MP3 file you want, and it won't delay the loading of your document. (The process used to "stream" MP3 files is actually progressive downloading, but the term stream is used because when you select the behavior from the Behaviors panel, you select the option Load Streaming MP3 File.) However, once an MP3 begins to play, the audio must take a little time to load a portion into the buffer.
This chapter is from Macromedia Flash MX Professional 2004, by Bill Sanders (Sams, 2004, ISBN: 0672326051). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Using the Sound Class >>
More Flash Articles
More By Bill Sanders