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

import flash.ev<strong>en</strong>ts.Ev<strong>en</strong>t;<br />

import flash.media.Sound;<br />

import flash.net.URLRequest;<br />

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

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

snd.load(req);<br />

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

channel.addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.SOUND_COMPLETE, onPlaybackComplete);<br />

public function onPlaybackComplete(ev<strong>en</strong>t:Ev<strong>en</strong>t)<br />

{<br />

trace("The sound has finished playing.");<br />

}<br />

The SoundChannel class does not dispatch progress ev<strong>en</strong>ts during playback. To report on playback progress, your<br />

application can set up its own timing mechanism and track the position of the sound playhead.<br />

To calculate what perc<strong>en</strong>tage of a sound has be<strong>en</strong> played, you can divide the value of the SoundChannel.position<br />

property by the l<strong>en</strong>gth of the sound data that’s being played:<br />

var playbackPerc<strong>en</strong>t:uint = 100 * (channel.position / snd.l<strong>en</strong>gth);<br />

However, this code only reports accurate playback perc<strong>en</strong>tages if the sound data was fully loaded before playback<br />

began. The Sound.l<strong>en</strong>gth property shows the size of the sound data that is curr<strong>en</strong>tly loaded, not the ev<strong>en</strong>tual size of<br />

the <strong>en</strong>tire sound file. To track the playback progress of a streaming sound that is still loading, your application should<br />

estimate the ev<strong>en</strong>tual size of the full sound file and use that value in its calculations. You can estimate the ev<strong>en</strong>tual<br />

l<strong>en</strong>gth of the sound data using the bytesLoaded and bytesTotal properties of the Sound object, as follows:<br />

var estimatedL<strong>en</strong>gth:int =<br />

Math.ceil(snd.l<strong>en</strong>gth / (snd.bytesLoaded / snd.bytesTotal));<br />

var playbackPerc<strong>en</strong>t:uint = 100 * (channel.position / estimatedL<strong>en</strong>gth);<br />

The following code loads a larger sound file and uses the Ev<strong>en</strong>t.ENTER_FRAME ev<strong>en</strong>t as its timing mechanism for<br />

showing playback progress. It periodically reports on the playback perc<strong>en</strong>tage, which is calculated as the curr<strong>en</strong>t<br />

position value divided by the total l<strong>en</strong>gth of the sound data:<br />

Last updated 6/6/2012<br />

452

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

Saved successfully!

Ooh no, something went wrong!