Placing sounds in the timeline (as streaming, start, stop or event sounds) is one way you can utilize audio in Flash. But it is also possible to trigger a sound stored in the library using Actionscript. To do this we must first create a sound object, kind of like a little code container for the sound. Then we write a handler (press, release, enterFrame, etc.) to trigger the sound at the appropriate time.
Right-click (ctrl-click) on the name of the sound object in the library window to open its contextual menu. Select Linkage, then check the "Export for Actionscript" option in the dialog box that appears.
In the same dialog box you will need to give the sound an identifier—a name that we use to refer to the sound file via Actionscript. By default the identifier will likely be the file name; however, you can change it. Identifiers can not start with a number or special character and must be unique. I'm going to call mine sound01_id.
Add a new layer and call it Actions. Click on the keyframe in frame 1 and open the script window. Then add this code:
_root.mySound01=new Sound();
_root.mySound01.attachSound("sound01_id");
The first line of code creates a sound object named mySound01. This is an arbitrary name that I gave it; you could call it whatever you like (as long as you use standard naming conventions and don't use a word/term that's already part of Actionscript).
The second line fills the newly created object with the chosen sound from the library (note the use of the linkage name). Note also the use of the attachSound command; this is used to refer to internal sounds (sounds in the library). External sounds can be referred to with the loadSound command (more on that later).
on (press) {
_root.mySound01.start(0,999);
}
This code uses an on(press) handler, but you could use a different handler if you prefer. The second line could be translated as "on the main timeline, find "mySound01" and start it playing." The two numbers in parenthesis refer to the offset and number of loops. Offset is the number of seconds that you want to skip when before the sound starts (in other words, if you want the sound to begin at :12, you would write (12, 999) after start). Of course, you can change the number of loops too.
on (press) {
_root.mySound01.stop();
}
I'm thinking this one is pretty self explanatory.
on (press) {
if (playing != true) {
playing = true;
_root.mySound01.start(0,999);
}
}
And add this to the "stop" button:
on (press) {
_root.mySound01.stop();
playing=false;
}
The if statement checks to see the status of the playing variable. If it is true, it ignores the command to start. However, if it is false, it starts the sound and then switches the variable's value to true.
Okay, what if you want multiple sounds? Create a new sound object for each one with a corresponding "start" button. But this can create some complications. First of all, you will only be able to play one sound at a time. While this may be desirable if you're playing songs, there are times when you will want to have multiple sounds play simultaneously. To do this you have to make a few minor changes to the code:
on (press) {
_root.mySound01.stop();
playing2=false;
playing1=false;
}
on (press) {
_root.mySound01.stop("sound01_id");
playing1=false;
}
An even more sophisticated method of manipulating multiple sounds is to attach each sound object to its own movie clip. These clips can be entirely empty, they just have to be placed on stage so that a script can be attached to them. This method allows for separate, independent volume controls, fader controls, etc. You could in fact create your own beat box/mixer as we do in this optional exercise.
Some possibilities:
10 pts |
Went above & beyond and made something special |
9 pts |
Above and beyond the basics. |
8 pts |
Did the basics; it works, but it's nothing fancy. |
7 pts |
Something's not working right. |
0-6 pts |
Poor showing; mostly incomplete or full of errors. |