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 />

Your application can override the global SoundMixer.bufferTime value for an individual sound by explicitly<br />

specifying a new bufferTime value wh<strong>en</strong> loading the sound. To override the default buffer time, first create a new<br />

instance of the SoundLoaderContext class, set its bufferTime property, and th<strong>en</strong> pass it as a parameter to the<br />

Sound.load() method, as shown below:<br />

import flash.media.Sound;<br />

import flash.media.SoundLoaderContext;<br />

import flash.net.URLRequest;<br />

var s:Sound = new Sound();<br />

var req:URLRequest = new URLRequest("bigSound.mp3");<br />

var context:SoundLoaderContext = new SoundLoaderContext(8000, true);<br />

s.load(req, context);<br />

s.play();<br />

As playback continues, Flash Player and AIR try to keep the sound buffer at the same size or greater. If the sound data<br />

loads faster than the playback speed, playback will continue without interruption. However, if the data loading rate<br />

slows down because of network limitations, the playhead could reach the <strong>en</strong>d of the sound buffer. If this happ<strong>en</strong>s,<br />

playback is susp<strong>en</strong>ded, though it automatically resumes once more sound data has be<strong>en</strong> loaded.<br />

To find out if playback is susp<strong>en</strong>ded because Flash Player or AIR is waiting for data to load, use the<br />

Sound.isBuffering property.<br />

Working with dynamically g<strong>en</strong>erated audio<br />

Flash Player 10 and later, Adobe AIR 1.5 and later<br />

Note: The ability to dynamically g<strong>en</strong>erate audio is available starting with Flash Player 10 and Adobe AIR 1.5.<br />

Instead of loading or streaming an existing sound, you can g<strong>en</strong>erate audio data dynamically. You can g<strong>en</strong>erate audio<br />

data wh<strong>en</strong> you assign an ev<strong>en</strong>t list<strong>en</strong>er for the sampleData ev<strong>en</strong>t of a Sound object. (The sampleData ev<strong>en</strong>t is defined<br />

in the SampleDataEv<strong>en</strong>t class in the flash.ev<strong>en</strong>ts package.) In this <strong>en</strong>vironm<strong>en</strong>t, the Sound object doesn’t load sound<br />

data from a file. Instead, it acts as a socket for sound data that is being streamed to it through the use of the function<br />

you assign to this ev<strong>en</strong>t.<br />

Wh<strong>en</strong> you add a sampleData ev<strong>en</strong>t list<strong>en</strong>er to a Sound object, the object periodically requests data to add to the sound<br />

buffer. This buffer contains data for the Sound object to play. Wh<strong>en</strong> you call the play() method of the Sound object,<br />

it dispatches the sampleData ev<strong>en</strong>t wh<strong>en</strong> requesting new sound data. (This is true only wh<strong>en</strong> the Sound object has not<br />

loaded mp3 data from a file.)<br />

The SampleDataEv<strong>en</strong>t object includes a data property. In your ev<strong>en</strong>t list<strong>en</strong>er, you write ByteArray objects to this data<br />

object. The byte arrays you write to this object add to buffered sound data that the Sound object plays. The byte array<br />

in the buffer is a stream of floating-point values from -1 to 1. Each floating-point value repres<strong>en</strong>ts the amplitude of one<br />

channel (left or right) of a sound sample. Sound is sampled at 44,100 samples per second. Each sample contains a left<br />

and right channel, interleaved as floating-point data in the byte array.<br />

In your handler function, you use the ByteArray.writeFloat() method to write to the data property of the<br />

sampleData ev<strong>en</strong>t. For example, the following code g<strong>en</strong>erates a sine wave:<br />

Last updated 6/6/2012<br />

448

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

Saved successfully!

Ooh no, something went wrong!