VCA224 Multimedia 2

Exercise: Sound Objects

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.

Specifications

Procedure

  1. You'll need a sound of some sort (obviously). Aiffs, wavs and mp3s work quite will with Flash. If you don't have a sound handy you can download this zipped file.
  2. Open a new Flash file and import the sound into the library.
  3. Create a couple of buttons to control the sound—an "on" and "off" button. Place them both on stage.
  4. Now we have to set the linkage for the sound. Why is this important? When you export a .swf file, everything in the timeline is packaged up and put in the .swf movie; however, objects in the library that are not used in a timeline are usually ignored as unnecessary. But if we're using sounds stored in the library we need to tell Flash to include them when it exports the .swf; that's where linkage comes in.
  5. 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.

  6. Define your sound object. Sound objects can be defined—set up—on the main timeline or within movie clips. In general it's usually best to define all of your sounds at the root level (which makes calling them with the code very easy, no long complicated urls pointing down to the movie clip that contains the sound).
  7. 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).

  8. Now we'll make the buttons work. First, select the "start" button and add this code:
  9. 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.

  10. On the "stop" button add this code:
  11. on (press) {
    _root.mySound01.stop();

    }

    I'm thinking this one is pretty self explanatory.

  12. Export a .swf and test it; does it work?
  13. Wait, there's a problem—if you hit the start button again before the sound finishes playing, another instance of the sound starts on top of the first, creating an auditory mess! How can we insure that we only hear one occurrence of the sound at a time? We need to add a function that checks to see if the sound is playing or not, and if it is, prevents the start button from working. Open the code on the "start" button and add the bits in red:
  14. 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.

Theme and variation

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:

  1. First, give each sound object a different "playing" variable (e.g. playing1, playing2, etc.).
  2. Next, if you have only one stop button, make sure it sets all of the "playing" variables to false (otherwise some sounds may get stuck "off"). The code might look something like this:
  3. on (press) {
    _root.mySound01.stop();
    playing2=false;
    playing1=false;

    }

  4. Note that even though the code says _root.mySound01, it stops all sounds, no matter what they're called. Why? This allows us to create global controls (controls that effect all sounds simultaneously). So what if you want to have multiple independent stop buttons, one for each sound? Add the linkage name for the sound to be stopped in between the parenthesis for each stop command. For example:
  5. 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.

Go Above and Beyond

Some possibilities:

Point Breakdown

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.

More advanced sound projects

Reference

 

Course Outline

Syllabus

Student Resource