Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > Adobe Flash

Building a Sound Mixer in Flash

  • Print
  • + Share This
From the author of

Building a Sound Mixer in Flash

By Allan Kennedy, author of the Programming Sound in Flash section of Macromedia Flash: Super Samurai

Once you understand the background theory on the sound object in relation to multiple sounds, you can put this information to use.

Open sndObj_multiple.swf from the CD-ROM that comes with Macromedia Flash Super Samurai.

This example, a sound mixer, demonstrates a simple yet effective way to control multiple sounds in your Flash movie while avoiding the headaches of using a global Sound object. You can change the volume and pan values for three sounds playing at the same time (Figure 1).

Figure 1 Use the buttons to control and mix the sounds.

Open sndObj_multiple.fla from the CD-ROM that comes with Macromedia Flash Super Samurai (or follow along in Flash, using your own files).

This file uses three movie clip symbol instances, each containing two sliders: one for volume and the other for pan control. Each Sound object is instantiated within these movie clip symbols. This lets you control each sound separately, or all three sounds at once, by using the larger buttons to the right. The code attached to each movie clip symbol lets you individually control each Sound object. Here's the code:

onClipEvent (load) {
 sComponent = new Sound(this);
 sComponent.attachSound(this._name);
 sComponent.playing = false;
 var loops = 500;
 var secondOffset = 0;
 var volume = 100;
 var pan = 0;	
}

Notice that the Sound object is instantiated with our new Sound() constructor function using this as a parameter value. The this keyword is a reference to the movie clip symbol instance and associates each Sound object with a unique timeline. Once the new Sound object is referenced through the variable sComponent, we can attach each sound from the Library by passing the movie clip symbol's instance name as the parameter for the attachSound() method. (All three movie clip symbols use the same variable name for the Sound object because each exists in different scopes.) I find it convenient to match my movie clip symbols' instance names to the identifier string used in linkage properties. In this case, I used s1, s2, and s3 for each movie clip symbol and linkage identifiers.

The following statements declare and initialize values that are needed by our start(), setVolume(), and setPan() methods. If the fadeOut() function hasn't been triggered by a button on the main timeline, the enterFrame clip event checks for any change in the volume and pan values.

onClipEvent (enterFrame) {
 sComponent.setPan(pan);
 if(fadeTrig) {
  fadeOut(); 
 }else {
  sComponent.setVolume(volume);
 }
}

If the fadeOut() function has been triggered, the enterFrame clip event repeatedly calls a function inside each movie clip symbol. This function looks at the current volume level for each Sound object and decreases it until volume is set to 0. When this occurs, the fade trigger is deactivated, and playing for that individual sound is set to false. This code is placed in the first frame of each movie clip's timeline:

function fadeOut () {
if (sComponent.getVolume()>=0) {
 sComponent.setVolume( sComponent.getVolume()-4);
} else {
 sComponent.setVolume(0);
 sComponent.stop();
 fadeTrig = sComponent.playing = false;
  }
}

We control each Sound object (starting, stopping, and use of sliders) just as we did with the single sound example, except we're no longer using a global Sound object. We now have three sounds that can be controlled independently via the sliders and buttons.

TIP

The maximum number of sounds that can safely be controlled simultaneously via the Sound object is debatable: Some say four; others insist on six. Ultimately, this depends on the capabilities of your sound card. Some low-end cards can play only eight individual sounds simultaneously. I recommend using no more than eight sounds, which is what I have safely worked with to date.

Allan Kennedy is a Flash and Director developer and digital media instructor at Toronto's award-winning zinc Roe design team (www.zincroe.com).

Allan is also a contributor to www.ultrashock.com and has given seminars on Flash and Director throughout the U.S. and Canada. He has worked on online Flash and Shockwave content for companies such as the Family Channel, Cage Digital, Teletoon, and CN Rail.

  • + Share This
  • 🔖 Save To Your Account