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

nc = new NetConnection();<br />

nc.connect(null);<br />

ns = new NetStream(nc);<br />

ns.addEv<strong>en</strong>tList<strong>en</strong>er(NetStatusEv<strong>en</strong>t.NET_STATUS, netStatusHandler);<br />

ns.cli<strong>en</strong>t = cli<strong>en</strong>t;<br />

The netStatusHandler() method is called wh<strong>en</strong>ever the status of the video is changed. This includes wh<strong>en</strong> a video<br />

starts or stops playback, is buffering or if a video stream cannot be found. The following code lists the<br />

netStatusHandler() ev<strong>en</strong>t:<br />

private function netStatusHandler(ev<strong>en</strong>t:NetStatusEv<strong>en</strong>t):void<br />

{<br />

try<br />

{<br />

switch (ev<strong>en</strong>t.info.code)<br />

{<br />

case "NetStream.Play.Start":<br />

t.start();<br />

break;<br />

case "NetStream.Play.StreamNotFound":<br />

case "NetStream.Play.Stop":<br />

t.stop();<br />

playNextVideo();<br />

break;<br />

}<br />

}<br />

catch (error:TypeError)<br />

{<br />

// Ignore any errors.<br />

}<br />

}<br />

The previous code evaluates the code property of the info object and filters whether the code is “NetStream.Play.Start”,<br />

“NetStream.Play.StreamNotFound”, or “NetStream.Play.Stop”. All other codes will be ignored. If the net stream is<br />

starting the code starts the Timer instance which updates the playhead. If the net stream cannot be found or is stopped,<br />

the Timer instance is stopped and the application attempts to play the next video in the playlist.<br />

Every time the Timer executes, the positionBar progress bar instance updates its curr<strong>en</strong>t position by calling the<br />

setProgress() method of the ProgressBar class and the positionLabel Label instance is updated with the time<br />

elapsed and total time of the curr<strong>en</strong>t video.<br />

private function timerHandler(ev<strong>en</strong>t:TimerEv<strong>en</strong>t):void<br />

{<br />

try<br />

{<br />

positionBar.setProgress(ns.time, meta.duration);<br />

positionLabel.text = ns.time.toFixed(1) + " of " meta.duration.toFixed(1) + " seconds";<br />

}<br />

catch (error:Error)<br />

{<br />

// Ignore this error.<br />

}<br />

}<br />

Last updated 6/6/2012<br />

509

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

Saved successfully!

Ooh no, something went wrong!