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

public function play(pos:int = 0):void<br />

{<br />

if (!this.isPlaying)<br />

{<br />

if (this.isReadyToPlay)<br />

{<br />

this.sc = this.s.play(pos);<br />

this.sc.addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.SOUND_COMPLETE, onPlayComplete);<br />

this.isPlaying = true;<br />

}<br />

}<br />

}<br />

this.playTimer = new Timer(this.progressInterval);<br />

this.playTimer.addEv<strong>en</strong>tList<strong>en</strong>er(TimerEv<strong>en</strong>t.TIMER, onPlayTimer);<br />

this.playTimer.start();<br />

The play() method calls the Sound.play() method if the sound is ready to play. The resulting SoundChannel object<br />

is stored in the sc property. The play() method th<strong>en</strong> creates a Timer object that will be used to dispatch playback<br />

progress ev<strong>en</strong>ts at regular intervals.<br />

Displaying playback progress<br />

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

Creating a Timer object to drive playback monitoring is complex operation that you should only have to code once.<br />

Encapsulating this Timer logic in a reusable class like the SoundFacade class lets applications list<strong>en</strong> to the same kinds<br />

of progress ev<strong>en</strong>ts wh<strong>en</strong> a sound is loading and wh<strong>en</strong> it is playing.<br />

The Timer object that is created by the SoundFacade.play() method dispatches a TimerEv<strong>en</strong>t instance every second.<br />

The following onPlayTimer() method executes wh<strong>en</strong>ever a new TimerEv<strong>en</strong>t arrives:<br />

public function onPlayTimer(ev<strong>en</strong>t:TimerEv<strong>en</strong>t):void<br />

{<br />

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

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

var progEv<strong>en</strong>t:ProgressEv<strong>en</strong>t =<br />

new ProgressEv<strong>en</strong>t(PLAY_PROGRESS, false, false, this.sc.position, estimatedL<strong>en</strong>gth);<br />

this.dispatchEv<strong>en</strong>t(progEv<strong>en</strong>t);<br />

}<br />

The onPlayTimer() method implem<strong>en</strong>ts the size estimation technique described in the section “Monitoring<br />

playback” on page 451. Th<strong>en</strong> it creates a new ProgressEv<strong>en</strong>t instance with an ev<strong>en</strong>t type of<br />

SoundFacade.PLAY_PROGRESS, with the bytesLoaded property set to the curr<strong>en</strong>t position of the SoundChannel<br />

object and the bytesTotal property set to the estimated l<strong>en</strong>gth of the sound data.<br />

Pausing and resuming playback<br />

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

The SoundFacade.play() method shown previously accepts a pos parameter corresponding to a starting position in<br />

the sound data. If the pos value is zero, the sound starts playing from the beginning.<br />

The SoundFacade.stop() method also accepts a pos parameter as shown here:<br />

Last updated 6/6/2012<br />

472

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

Saved successfully!

Ooh no, something went wrong!