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

Loading video files<br />

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

Loading videos using the NetStream and NetConnection classes is a multistep process:<br />

1 Create a NetConnection object. If you are connecting to a local video file or one that is not using a server such as<br />

Adobe's Flash Media Server 2, pass null to the connect() method to play the video files from either an HTTP<br />

address or a local drive. If you are connecting to a server, set the parameter to the URI of the application that<br />

contains the video file on the server.<br />

var nc:NetConnection = new NetConnection();<br />

nc.connect(null);<br />

2 Create a NetStream object which takes a NetConnection object as a parameter and specify the video file that you<br />

want to load. The following snippet connects a NetStream object to the specified NetConnection instance and loads<br />

a video file named video.mp4 in the same directory as the SWF file:<br />

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

ns.addEv<strong>en</strong>tList<strong>en</strong>er(AsyncErrorEv<strong>en</strong>t.ASYNC_ERROR, asyncErrorHandler);<br />

ns.play("video.mp4");<br />

function asyncErrorHandler(ev<strong>en</strong>t:AsyncErrorEv<strong>en</strong>t):void<br />

{<br />

// ignore error<br />

}<br />

3 Create a new Video object and attach the previously created NetStream object using the Video class’s<br />

attachNetStream() method. Th<strong>en</strong> you can add the video object to the display list using the addChild() method,<br />

as se<strong>en</strong> in the following snippet:<br />

var vid:Video = new Video();<br />

vid.attachNetStream(ns);<br />

addChild(vid);<br />

As Flash Player executes this code it attempts to load the video.mp4 video file from the same directory as your SWF file.<br />

More Help topics<br />

Flex: Spark VideoPlayer control<br />

spark.compon<strong>en</strong>ts.VideoDisplay<br />

Controlling video playback<br />

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

The NetStream class offers four main methods for controlling video playback:<br />

pause(): Pauses playback of a video stream. If the video is already paused, calling this method does nothing.<br />

resume(): Resumes playback of a video stream that is paused. If the video is already playing, calling this method does<br />

nothing.<br />

seek(): Seeks the keyframe closest to the specified location (an offset, in seconds, from the beginning of the stream).<br />

togglePause(): Pauses or resumes playback of a stream.<br />

Last updated 6/6/2012<br />

479

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

Saved successfully!

Ooh no, something went wrong!