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

Monitoring the sound loading process<br />

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

Sound files can be very large and take a long time to load. While Flash Player and AIR let your application play sounds<br />

ev<strong>en</strong> before they are fully loaded, you might want to give the user an indication of how much of the sound data has<br />

be<strong>en</strong> loaded and how much of the sound has already be<strong>en</strong> played.<br />

The Sound class dispatches two ev<strong>en</strong>ts that make it relatively easy to display the loading progress of a sound:<br />

ProgressEv<strong>en</strong>t.PROGRESS and Ev<strong>en</strong>t.COMPLETE. The following example shows how to use these ev<strong>en</strong>ts to display<br />

progress information about the sound being loaded:<br />

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

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

import flash.media.Sound;<br />

import flash.net.URLRequest;<br />

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

s.addEv<strong>en</strong>tList<strong>en</strong>er(ProgressEv<strong>en</strong>t.PROGRESS, onLoadProgress);<br />

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

s.addEv<strong>en</strong>tList<strong>en</strong>er(IOErrorEv<strong>en</strong>t.IO_ERROR, onIOError);<br />

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

s.load(req);<br />

function onLoadProgress(ev<strong>en</strong>t:ProgressEv<strong>en</strong>t):void<br />

{<br />

var loadedPct:uint = Math.round(100 * (ev<strong>en</strong>t.bytesLoaded / ev<strong>en</strong>t.bytesTotal));<br />

trace("The sound is " + loadedPct + "% loaded.");<br />

}<br />

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

{<br />

var localSound:Sound = ev<strong>en</strong>t.target as Sound;<br />

localSound.play();<br />

}<br />

function onIOError(ev<strong>en</strong>t:IOErrorEv<strong>en</strong>t)<br />

{<br />

trace("The sound could not be loaded: " + ev<strong>en</strong>t.text);<br />

}<br />

This code first creates a Sound object and th<strong>en</strong> adds list<strong>en</strong>ers to that object for the ProgressEv<strong>en</strong>t.PROGRESS and<br />

Ev<strong>en</strong>t.COMPLETE ev<strong>en</strong>ts. After the Sound.load() method has be<strong>en</strong> called and the first data is received from the sound<br />

file, a ProgressEv<strong>en</strong>t.PROGRESS ev<strong>en</strong>t occurs and triggers the onSoundLoadProgress() method.<br />

The perc<strong>en</strong>tage of the sound data that has be<strong>en</strong> loaded is equal to the value of the bytesLoaded property of the<br />

ProgressEv<strong>en</strong>t object divided by the value of the bytesTotal property. The same bytesLoaded and bytesTotal<br />

properties are available on the Sound object as well. The example above simply shows messages about the sound<br />

loading progress, but you can easily use the bytesLoaded and bytesTotal values to update progress bar compon<strong>en</strong>ts,<br />

such as the ones that come with the Adobe Flex framework or the Adobe Flash authoring tool.<br />

This example also shows how an application can recognize and respond to an error wh<strong>en</strong> loading sound files. For<br />

example, if a sound file with the giv<strong>en</strong> fil<strong>en</strong>ame cannot be located, an Ev<strong>en</strong>t.IO_ERROR ev<strong>en</strong>t is dispatched by the<br />

Sound object. In the previous code, the onIOError() method executes and displays a brief error message wh<strong>en</strong> an<br />

error occurs.<br />

Last updated 6/6/2012<br />

445

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

Saved successfully!

Ooh no, something went wrong!