Back to Entry Page

Using the Sound Object

ActionScript's built-in objects include Sound, an object that offers finer and more flexible control of sound than is available through the timeline. The Sound object is entirely independent of the timeline. Sound objects have nothing to do with keyframes or palette settings; they are managed entirely within scripts.

Several steps are required in order to use a Sound object. The first of these steps is the same as for timeline sound: you acquire a digital sound recording (e.g., a WAV file) and use File>Import to include it in your source (.fla) file.

Setting Linkage for a digital sound in the Library

For object sound, however, you must add a subsequent step after importing. As shown in the figure at left, find your sound file in the Library and right-click (or for Mac users, click-and-hold) on its icon. Select Linkage from the pop-up menu.

A new dialog box will appear. In that box, click the radio button labeled Export This Symbol. The fill-in field above the radio buttons will become active after you do this. In that field, enter a name by which you will refer to this sound in scripting. The name can be anything you like, though it should not include spaces or punctuation.

Before you can control sound with an object, you must first create the object. This is done with a statement called a constructor, which looks like this:

mySound = new Sound(_root);

This statement creates an instance of the Sound object and attaches it to the highest organizational level of your movie, the root. If you want to use multiple object sounds in your movie, you may want to substitute this for _root, and insert the constructor into the script for some lower-level symbol, such as a Movie Clip (which then becomes the reference for this). The name "mySound" here is arbitrary.

Once you've set up your Sound object, you need to connect it to your digitized sound file. Use the attachSound() method of the Sound object, like this:

mySound.attachSound("name");

Where you see "name" above, substitute the name you gave the sound source when you set its Linkage property.

To make the sound play, use the start() method of the Sound object, thus:

mySound.start(0,999);

You may leave the parameters of the start() method blank, but here we give two values. The first (0) indicates offset, an amount of sound content that will be skipped before playback begins. You can use offset to play a long sound file from the middle. In this case we're using no offset, so the sound plays from the start.

The second parameter (999) controls looping. This statement will play the sound attached to mySound 1000 times: once plus 999 repetitions. There is no way to make object sounds loop infinitely, and some reference books warn against using values greater than 999. For Web purposes, 1000 iterations of a sound should be plenty, since people tend not to stay on Web pages all that long. For true infinite loops you'll have to use timeline sound.

There are several other methods for the Sound object, the most important of which is probably setVolume(). This does just what the name implies. Here's an example:

mySound.setVolume(50);

The parameter indicates volume of sound playback. According to the Flash reference, it represents a percentage of the maximum output level available to your system--but since values above 100 continue to produce louder sounds, this can't be true. In any case, a higher number means a louder sound. You can use the setVolume() method at any point in your movie, meaning that you can change the volume of an object-driven sound in response to user action. This represents a crucial improvement over timeline sound.

The demonstration project below shows two fairly simple applications of object sound: a draggable volume control (the slider at right) and an automatic timed fade-out (button at left). Neither of these effects could be achieved as you see them here using timeline sound.

The source file for this demo is called autoFade.fla and it may be downloaded from 610-Shared on student-iat.ubalt.edu.


University of Baltimore Logo

Copyright © 2002 School of Information Arts and Technologies