424
Chapter 21: Working with sound
ActionScript™ is made for immersive, interactive applications—and one often overlooked element of powerfully
immersive applications is sound. You can add sound effects to a video game, audio feedback to an application user
interface, or even make a program that analyzes mp3 files loaded over the Internet, with sound at the core of the
application.
In this chapter, you’ll learn about loading external audio files and working with audio that’s embedded in a SWF.
You’ll learn to control the audio, to create visual representations of the sound information, and to capture sound from
a user’s microphone.
Contents
Basics of working with sound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Understanding the sound architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Loading external sound files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Working with embedded sounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Working with streaming sound files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Playing sounds. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Security considerations when loading and playing sounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Controlling sound volume and panning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Working with sound metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Accessing raw sound data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Capturing sound input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Example: Podcast Player . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Basics of working with sound
Introduction to working with sound
Just as computers can encode images in a digital format, store them on a computer, and retrieve them to display on
the screen, computers can capture and encode digital audio—computer representation of sound information—and
can store it and retrieve it to play back over speakers connected to the computer. You can play back sound using either
Adobe® Flash® Player or Adobe® AIR™ and ActionScript.
When sound data is converted to digital form, it has various characteristics, such as the sounds volume and whether
it is stereo or mono sound. When you play back a sound in ActionScript, you can adjust these characteristics as
well—make the sound louder, or make it seem to be coming from a certain direction, for instance.
Before you can control a sound in ActionScript, you need to have the sound information loaded into Flash Player or
AIR. There are four ways you can get audio data into Flash Player or AIR so that you can work with it using Action-
Script. You can load an external sound file such as an mp3 file into the SWF; you can embed the sound information
into the SWF file directly when its being created; you can get audio input using a microphone attached to a user’s
computer, and you can access sound data thats streamed from a server.
When you load sound data from an external sound file, you can begin playing back the start of the sound file while
the rest of the sound data is still loading.

Get ADOBE® FLEX® 3: PROGRAMMING ACTIONSCRIPT™ 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.