ActionScripting for Designers: Controlling Audio
- Sound Objects
- Functions
- Controlling Sound Object Volume with ActionScript
Objects and classes are the heart of ActionScript. Think of objects as data types you create to control your Flash movies. Typical Flash objects include movie clips, sounds, graphics, and text. All the objects you use and create belong to a larger collective group called a class.
Flash provides some built-in classes for handling Flash elements including data, sound, and video. Learning to code in ActionScript is really all about understanding the capabilities of objects and their classes and using them to perform all sorts of interactive tasks.
In this article I will focus on the Flash Sound object. 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.
Up to this point, the sounds you have used in your Flash projects have probably been added to keyframes. You create a new keyframe in the timeline where you want your sound to start playing, select this keyframe, and then drag the sound from the Library onto the stage. The sound will play when the playback head reaches the frame where the sound clip is located.
However, what if you need to control the sound dynamically at runtime in order to change playback based on user input, change its volume, pan from left to right, or determine how long it has been playing? In all these cases you will need to use an ActionScript Sound object.
Sound Objects
Flash Sound objects are a bit abstract, but they are such useful and powerful tools that it is 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 actually 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 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.