Controlling Flash Audio Volume with Sound Objects
- What are Sound Objects?
- Creating the Sound Object
- Controlling the Sound Object with ActionScript
If you work with audio in Flash, you know that attaching audio to a keyframe is an easy way to incorporate sounds into your movies. You can use the Event sync to play a sound effect in the down state of a button and Stream sync to synchronize sound to a stage animation.
But what if you need to control sound dynamically at runtime to change its volume, pan from left to right, or determine how long it has been playing? You need to use an ActionScript Sound object.
In this article I will show you how to create Sound objects and use them to control the volume of an audio file playing not from a timeline, but from within the library.
What are Sound Objects?
Flash Sound objects are a bit abstract, but they are such useful and powerful tools that it's definitely worthwhile knowing more about what they are and how they work.
A Sound object is not the actual sound used in the Flash file; it is simply a reference to the sound resources you will be using. Think of it as a translator between a sound’s properties—such as volume, balance, or duration—and the actual sound in the library. ActionScript attaches the sound resource to the Flash movie at runtime. Sound objects make it possible to dynamically play sounds any time and anywhere because they eliminate the necessity of placing sounds in keyframes on a timeline.
A Sound object is composed of three elements:
- A sound file from the library
- A Sound instance created with a constructor function
- A MovieClip object or timeline that stores the attached sound file
If, as demonstrated here, you need to control only one sound in your movie, you don’t need to use a movie clip object. However, if you want to control multiple Sound objects, you need to store all your attached sound files in MovieClip objects.
After a Sound object has been created and named using the constructor function, you associate, or link, a sound file to it in the library. After the Sound object has been named and linked, it is ready to be controlled with ActionScript. You can use ActonScript to play, stop, pan, and control a variety of other sound properties. However, the most common application is to control sound volume.
Let me demonstrate all this theory by taking you step by step through the creation, linking, and controlling of a Sound object.