The Behaviors panel provides several pre-built scripts for controlling sound objects in a movie, including Play Sound, Stop Sound and Stop All Sounds. But in order to use the Play Sound behavior, you need to first load the sound using the Load Sound from Library or Load Streaming MP3 File behavior.
Unlike other symbols in the library, sound instances are given linkage identifiers, rather than names (though for all intents and purposes, they’re the same). You use these identifiers to later reference the sound. A sound must have a linkage identifier before you can load it into the movie with a behavior. To create a linkage identifier for a sound in the library:
Right-click on the sound.
From the shortcut menu, select Linkage.
In the Linkage Properties dialog, enter a new identifier or keep the default one, which is based on the sound’s file name.
Select the Export for ActionScript check box.
Click OK.
You can now load the sound into the movie using a behavior:
If you haven’t already done so, create a layer in your movie called “Actions”.
Select the first frame of the Actions layer.
In the Behaviors panel, click the Add Behavior button.
From the menu, select Sound and then Load Sound from Library.
In the Load Sound from Library dialog, enter the linkage identifier (linkage ID) you gave the sound earlier. This behavior will create an instance of the sound, so the instance needs a name. Type it into the Identifier field:
Keep the Play this sound when loaded check box checked to automatically start playing the sound when the movie loads (otherwise, uncheck it).
Click OK.
The behavior now appears in the Behaviors panel:
Notice that the top of the Behaviors panel tells you that the code for the behavior is attached to frame 1 of the Actions layer.
If you like, take a look at the code itself in the Actions panel:
That’s a good deal of code you didn’t have to write!
Note:
You can use the Load Streaming MP3 File behavior to load a sound from a web site. Instead of entering a linkage ID for the sound, you enter a URL, and then give the instance a unique name.
You should use one of these two load sound methods to load each sound you want to attach to an object in your movie using a behavior. Then, with sound identifiers in hand, you can add behaviors to buttons to allow the user to play and stop the sounds:
Add some buttons to your Stage. Below, we’ve brought up our trusty motion tween with the car (the simple one, without the fire hydrant). This movie has buttons that we programmed earlier to play and stop the movement of the car:
We’re now going to add behaviors to start a song playing when the user clicks the button. The song will play while the car moves, and stop with the car when the user presses the stop button.
First, we need to stop the sound from playing automatically. Earlier, when we loaded the sound, we left the default Play this sound when loaded check box selected. With a play button, we don’t want the sound to play automatically. To change the behavior, select the first frame of the Actions layer and then, in the Behaviors panel, double-click the load sound behavior.
This re-opens the Load Sound from Library dialog. Clear the check box and click OK.
Now, select the play button on the Stage.
In the Behaviors panel, click the Add Behavior button and select Sound and then Play Sound.
In the Play Sound dialog, enter the name of the sound instance you want to play. Click OK.
The event appears in the Behaviors panel:
Remember, you can select a different mouse event to trigger the event by clicking inside the Event column and selecting an event from the menu.
Next, select the stop button.
In the Behaviors panel, click the Add Behavior button and select Sound and then Stop Sound.
In the Stop Sound dialog, enter the linkage ID and the name of the sound instance you want to stop. Click OK.
The behavior appears in the Behaviors panel.
Test the movie, clicking the play and stop buttons to make sure the sounds play and stop.
Take a look at the code Flash inserted for the play button (select the play button and then open the Actions panel). Place the cursor inside the parentheses for the start method and then click the Show Code Hint button to see what parameters are being used:
You can change these two parameters to adjust the number of seconds offset and the
number of loops.