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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Working with sound<br />

Using the @Embed directive in MXML to assign an embedded asset as a property of a compon<strong>en</strong>t like a Button or a<br />

SoundEffect.<br />

Using the @Embed directive within a CSS file<br />

This section describes the first option: how to embed sounds in ActionScript code within a Flex application using the<br />

[Embed] metadata tag.<br />

To embed an asset in ActionScript code, use the [Embed] metadata tag.<br />

Place the sound file in the main source folder or another folder that is in your project’s build path. Wh<strong>en</strong> the compiler<br />

<strong>en</strong>counters an Embed metadata tag, it creates the embedded asset class for you. You can access the class through a<br />

variable of data type Class that you declare immediately after the [Embed] metadata tag.<br />

The following code embeds a sound named smallSound.mp3 and uses a variable named soundClass to store a<br />

refer<strong>en</strong>ce to the embedded asset class associated with that sound. The code th<strong>en</strong> creates an instance of the embedded<br />

asset class, casts it as an instance of the Sound class, and calls the play() method on that instance:<br />

package<br />

{<br />

import flash.display.Sprite;<br />

import flash.media.Sound;<br />

import flash.media.SoundChannel;<br />

}<br />

public class EmbeddedSoundExample ext<strong>en</strong>ds Sprite<br />

{<br />

[Embed(source="smallSound.mp3")]<br />

public var soundClass:Class;<br />

}<br />

public function EmbeddedSoundExample()<br />

{<br />

var smallSound:Sound = new soundClass() as Sound;<br />

smallSound.play();<br />

}<br />

To use the embedded sound to set a property of a Flex compon<strong>en</strong>t, it should be cast as an instance of the<br />

mx.core.SoundAsset class instead of as an instance of the Sound class. For a similar example that uses the SoundAsset<br />

class see “Embedded asset classes” in Learning ActionScript 3.0.<br />

Working with streaming sound files<br />

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

Wh<strong>en</strong> a sound file or video file is playing back while its data is still being loaded, it is said to be streaming. External<br />

sound files that are loaded from a remote server are oft<strong>en</strong> streamed so that the user doesn’t have to wait for all the<br />

sound data to load before list<strong>en</strong>ing to the sound.<br />

The SoundMixer.bufferTime property repres<strong>en</strong>ts the number of milliseconds of sound data that Flash Player or AIR<br />

should gather before letting the sound play. In other words, if the bufferTime property is set to 5000, Flash Player or<br />

AIR loads at least 5000 milliseconds worth of data from the sound file before the sound begins to play. The default<br />

SoundMixer.bufferTime value is 1000.<br />

Last updated 6/6/2012<br />

447

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

Saved successfully!

Ooh no, something went wrong!