13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with sound<br />

Working with embedded sounds<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

Using embedded sounds, instead of loading sound from an external file, is most useful for small sounds that are used<br />

as indicators within your application’s user interface, such as sounds that play wh<strong>en</strong> buttons are clicked.<br />

Wh<strong>en</strong> you embed a sound file in your application, the size of the resulting SWF file increases by the size of the sound<br />

file. In other words, embedding large sound files in your application can increase the size of your SWF file to an<br />

undesirable size.<br />

The exact method of embedding a sound file into your application’s SWF file varies according to your developm<strong>en</strong>t<br />

<strong>en</strong>vironm<strong>en</strong>t.<br />

Using an embedded sound file in Flash<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

The Flash authoring tool lets you import sounds in a number of sound formats and store them as symbols in the<br />

Library. You can th<strong>en</strong> assign them to frames in the timeline or to the frames of a button state, use them with Behaviors,<br />

or use them directly in ActionScript code. This section describes how to use embedded sounds in ActionScript code<br />

with the Flash authoring tool. For information about the other ways to use embedded sounds in Flash, see “Importing<br />

Sounds” in Using Flash.<br />

To embed a sound file using the Flash authoring tool:<br />

1 Select File > Import > Import to Library, and th<strong>en</strong> select a sound file and import it.<br />

2 Right-click the name of the imported file in the Library panel, and select Properties. Click the Export for<br />

ActionScript checkbox.<br />

3 In the Class field, <strong>en</strong>ter a name to use wh<strong>en</strong> referring to this embedded sound in ActionScript. By default, it will use<br />

the name of the sound file in this field. If the fil<strong>en</strong>ame includes a period, as in the name “DrumSound.mp3”, you<br />

must change it to something like “DrumSound”; ActionScript does not allow a period character in a class name.<br />

The Base Class field should still show flash.media.Sound.<br />

4 Click OK. You might see a dialog box saying that a definition for this class could not be found in the classpath. Click<br />

OK and continue. If you <strong>en</strong>tered a class name that doesn’t match the name of any of the classes in your application’s<br />

classpath, a new class that inherits from the flash.media.Sound class is automatically g<strong>en</strong>erated for you.<br />

5 To use the embedded sound, you refer<strong>en</strong>ce the class name for that sound in ActionScript. For example, the<br />

following code starts by creating a new instance of the automatically g<strong>en</strong>erated DrumSound class:<br />

var drum:DrumSound = new DrumSound();<br />

var channel:SoundChannel = drum.play();<br />

DrumSound is a subclass of the flash.media.Sound class so it inherits the Sound class’s methods and properties,<br />

including the play() method as shown above.<br />

Using an embedded sound file in Flex<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

There are many ways to embed sound assets in a Flex application, including:<br />

Using the [Embed] metadata tag in a script<br />

Last updated 6/6/2012<br />

446

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!