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

Home > Articles > Web Design & Development > Adobe Flash

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Attaching Sound Dynamically

The second—and more recent—way of using audio in your movies is to attach them dynamically. This method relies on some simple ActionScript and Flash's Symbol Linkage feature. It is a little more difficult to set up, but it offers the flexibility of scripted effects and timing that static sound does not.

Because dynamic sounds are created and controlled with ActionScript, they can be started and stopped in response to user input, and the volume and panning can also be altered. (Panning changes the balance between left and right speakers, which is useful for environment effects.)

Before a sound can be attached with script, it needs a little preparation by way of its Linkage settings. When exporting to SWF, Flash automatically leaves out any symbol or asset that isn't utilized at design time—and dynamic sounds fall under that category. Linkage offers a way to force sounds and movie clips to export, no matter what.

To link a sound asset, select it in the Library and open up its Linkage properties. Select Export this symbol and enter an Identifier in the text field. The Identifier is used to "identify" the sound when referring to it with ActionScript. It can be the same as the sound name, or it can be different.

When it's certain that the sound will be exported with the final SWF, the sound can be attached with ActionScript and played like so:

// create a new sound object
MySound = new Sound();

// attach the sound
MySound.attachSound("SoundID");

// start the sound playing
MySound.start();

The first line of the inserted code creates a new sound object. The second line of code attaches the sound to the timeline, and the last line starts the sound playing. For additional methods and options related to the Sound object, refer to the Flash user's guide.

  • + Share This
  • 🔖 Save To Your Account