View comments | RSS feed

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. One way to play back sound is using Adobe Flash Player and ActionScript.

When sound data is converted to digital form, it has various characteristics, such as the sound's 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. There are four ways you can get audio data into Flash Player so that you can work with it using ActionScript. 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 it's being created; you can get audio input using a microphone attached to a user's computer, and you can access sound data that's 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.

Although there are various sound file formats used to encode digital audio, ActionScript 3.0 and Flash Player support sound files that are stored in the mp3 format. They cannot directly load or play sound files in other formats like WAV or AIFF.

While you're working with sound in ActionScript, you'll likely work with several classes from the flash.media package. The Sound class is the class you use to get access to audio information by loading a sound file and starting playback. Once you start playing a sound, Flash Player gives you access to a SoundChannel object. Since an audio file that you've loaded may only be one of several sounds that you play on a user's computer, each individual sound that's playing uses its own SoundChannel object; the combined output of all the SoundChannel objects mixed together is what actually plays over the computer's speakers. You use this SoundChannel instance to control properties of the sound and to stop its playback. Finally, if you want to control the combined audio, the SoundMixer class gives you control over the mixed output.

You can also use several other classes to perform more specific tasks when you're working with sound in ActionScript; for more information on all the sound-related classes, see Understanding the sound architecture.

Common tasks for working with sound

This chapter describes the following sound-related tasks that you will likely want to perform:

Important concepts and terms

The following reference list contains important terms that you will encounter in this chapter:

Working through in-chapter examples

As you're working through this chapter, you may want to try out some of the example code listings. Since this chapter covers working with sound in ActionScript, many of the examples do something that involves working with a sound file--making it play, stopping playback, or adjusting the sound in some way. To test the examples in this chapter:

  1. Create a new Flash document and save it on your computer.
  2. In the Timeline, select the first keyframe and open the Actions panel.
  3. Copy the example code listing into the Script pane.
  4. If the code involves loading an external sound file, it will have a line of code that looks something like this:
    var req:URLRequest = new URLRequest("click.mp3");
    var s:Sound = new Sound(req);
    

    where "click.mp3" is the name of the sound file being loaded. In order to test these examples, you'll need to have an mp3 file to use. You should put the mp3 file in the same folder as your Flash document. You should then alter the code to use the name of your mp3 file instead of the name in the code listing (for example, in the code above you'd change "click.mp3" to the name of your mp3 file).

  5. From the main menu, choose Control > Test Movie to create the SWF file and preview (and hear) the output of the example.

In addition to playing audio, some of the examples display values using the trace() function; when you're testing those examples, you'll see the results of those values in the Output panel. Some examples also draw content to the screen, so for those examples you'll see the content in the Flash Player window as well.

For more information about testing the example code listings in this manual, see Testing in-chapter example code listings.


Flash CS3


Comments


Templeta said on Aug 13, 2007 at 9:37 AM :
I'm new to Actionscript 3.0 and I've copied the code var req:URLRequest = new URLRequest("click.mp3");
var s:Sound = new Sound(req);

in the requested place... in the actions panel after seleting frame 1 in the timeline. Everything is in place, but I don't hear the mp3 in the test movie.
I placed the fla file in a folder on my document and the mp3 file is also in that location. I also renamed the mp3 file to "click.mp3" so that it reflects the actionscript used in the action panel.
TonyV125 said on Oct 29, 2007 at 6:16 PM :
The example on this page under "Working through in-chapter examples" is missing the command to actually play the sound. There should be a third line that reads:

s.play();

The entire code sample should look like the following:

var req:URLRequest = new URLRequest("click.mp3");
var s:Sound = new Sound(req);
s.play();
Ludatic said on Feb 14, 2008 at 10:29 AM :
It says with the common tasks, it's possible to capture and replay sounds from the users microphone. However everywhere on the internet I read it is not possible, without first re-routing the sound to the back-end.
How would we be able to record sound from the microphone into a byte array?
I for one would like people to be able to make their own sound effects in a game and store them for later playback. I could think of a thousand other uses.
An example would be hugely appreceated!

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000284.html