Recipe 16.1.
Loading and Playing Back Video
Problem
You want
to load and playback progressive download Flash video.
Solution
Use a NetStream object to load and
playback the video and use a Video object to display the
video.
Discussion
ActionScript 3.0 requires several classes
working together to load and playback Flash video. You must use a
NetStream object to
load the video and control the playback; however, the
NetStream class is only concerned with moving data, it
doesn't know how to render the data as a video. For that you have
to use a Video object. The Video object allows you to
pass it a NetStream object, which then uses the
NetStream data to render the video to the screen.
The NetStream constructor requires that
you pass it a NetConnection object. The NetConnection
object determines the origin of the data that the NetStream
object handles. When the Flash video streams from a Flash
Communication Server or Flash Media Server, the
NetConnection object points to the server. However, for
progressive download video content the NetConnection object
uses a null connection string. The following code constructs
a NetConnection object and initializes it for use with
progressive download video. Note that the code assumes you've
imported the flash.net.NetConnection class:
var videoConnection:NetConnection = new NetConnection( );
videoConnection.connect(null);
Once you've constructed a NetConnection
object and called the connect( ) method with a
null value, construct the NetStream object, and
then pass the NetStream constructor a reference to the
NetConnection object. The following code constructs a
NetStream object (assuming you've imported
flash.net.NetStream):
var videoStream:NetStream = new NetStream(videoConnection);
Once you've constructed the NetStream
object, add a Video object, and associate the
NetStream with the Video.
The
flash.media.Video class is a display object, which means
that you have to add it to the display list once constructed. The
following code constructs a new Video object and then adds
it to the display list:
var video:Video = new Video( );
addChild(video);
You can associate a NetStream object with
a Video object by using the Video object's
attachNetStream( )
method, passing a reference to the NetStream object as a
parameter to the method:
video.attachNetStream(videoStream);
After you attached the NetStream object
to the Video object, any video data controlled by the
NetStream object gets rendered by the Video
object.
Everything discussed up to this point is
necessary to initialize all the requisite objects and associations.
However, nothing so far actually tells the Flash Player to load the
video and start playbackboth of which are accomplished with one
simple method call. The NetStream class defines a play(
) method that loads and starts the Flash video playback as
specified by the parameter passed to it. The parameter can be a
relative or absolute URL. The following tells the Flash Player to
load and start the playback of a Flash video called
example.flv that's in the same directory as the calling
.swf:
videoStream.play("example.flv");
If the .flv file is in the same domain as
the calling .swf, then the play( ) call isn't subject
to Flash Player security. However, if the FLV is in a different
domain, then a security policy file is required to allow the
calling SWF to load and playback the FLV.
Flash video playback starts automatically as
soon as enough has buffered. You can control buffering and monitor
loading, both of which are discussed in Recipe
16.7.
If the FLV file has metadata embedded within it
(and most encoders do embed metadata) you'll also have to handle
the metadata event as described in Recipe
16.4. Additionally, if the FLV contains cue points, you'll have
to handle the cue point events as described in Recipe
16.8. If the FLV contains metadata and/or cue points and you
don't handle the events, Flash Player throws errors.
See Also
Recipes 16.4,
16.7,
and 16.8
|