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

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

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

var urlReq:URLRequest = new URLRequest("test.mp3");<br />

sourceSnd.load(urlReq);<br />

sourceSnd.addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.COMPLETE, loaded);<br />

function loaded(ev<strong>en</strong>t:Ev<strong>en</strong>t):void<br />

{<br />

mySound.addEv<strong>en</strong>tList<strong>en</strong>er(SampleDataEv<strong>en</strong>t.SAMPLE_DATA, processSound);<br />

mySound.play();<br />

}<br />

function processSound(ev<strong>en</strong>t:SampleDataEv<strong>en</strong>t):void<br />

{<br />

var bytes:ByteArray = new ByteArray();<br />

sourceSnd.extract(bytes, 8192);<br />

ev<strong>en</strong>t.data.writeBytes(upOctave(bytes));<br />

}<br />

function upOctave(bytes:ByteArray):ByteArray<br />

{<br />

var returnBytes:ByteArray = new ByteArray();<br />

bytes.position = 0;<br />

while(bytes.bytesAvailable > 0)<br />

{<br />

returnBytes.writeFloat(bytes.readFloat());<br />

returnBytes.writeFloat(bytes.readFloat());<br />

if (bytes.bytesAvailable > 0)<br />

{<br />

bytes.position += 8;<br />

}<br />

}<br />

return returnBytes;<br />

}<br />

Limitations on g<strong>en</strong>erated sounds<br />

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

Wh<strong>en</strong> you use a sampleData ev<strong>en</strong>t list<strong>en</strong>er with a Sound object, the only other Sound methods that are <strong>en</strong>abled are<br />

Sound.extract() and Sound.play(). Calling any other methods or properties results in an exception. All methods<br />

and properties of the SoundChannel object are still <strong>en</strong>abled.<br />

Playing sounds<br />

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

Playing a loaded sound can be as simple as calling the Sound.play() method for a Sound object, as follows:<br />

var snd:Sound = new Sound(new URLRequest("smallSound.mp3"));<br />

snd.play();<br />

Wh<strong>en</strong> playing back sounds using ActionScript 3.0, you can perform the following operations:<br />

Play a sound from a specific starting position<br />

Pause a sound and resume playback from the same position later<br />

Last updated 6/6/2012<br />

450

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

Saved successfully!

Ooh no, something went wrong!