Loading FLV files in ActionScript 3.0 using the NetConnection and NetStream classes

by Peter deHaan on February 26, 2008

in Video, migration

The following example shows how you can load external FLV files using ActionScript 2.0 and ActionScript 3.0.

Full code after the jump.

The following example shows how you can load an .FLV file in ActionScript 2.0 using the NetConnection and NetStream classes and attach the video stream to the Video object on the Stage.

// ActionScript 2.0
/**
 * Requires:
 *   - Video object on the Stage with an instance name of "video".
 */
var nc:NetConnection = new NetConnection();
nc.connect(null);
 
var ns:NetStream = new NetStream(nc);
ns.onMetaData = function (item:Object):Void {
    trace("metaData");
    // Resize video instance.
    video._width = item.width;
    video._height = item.height;
    // Center video instance on Stage.
    video._x = (Stage.width - video._width) / 2;
    video._y = (Stage.height - video._height) / 2;
};
ns.onCuePoint = function (item:Object):Void {
    trace("cuePoint");
    trace(item.name + "\t" + item.time);
};
ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv");
 
video.attachVideo(ns);

In ActionScript 3.0, the code is a little different. Instead of creating a new Video object in the Flash document’s Library panel, you can create a new Video instance using the Video class’s constructor. Also, note that instead of going <NetStream>.onMetaData = function(...);, you attach the onMetaData and onCuePoint event handlers to a client object.

// ActionScript 3.0
var video:Video = new Video();
addChild(video);
 
var nc:NetConnection = new NetConnection();
nc.connect(null);
 
var ns:NetStream = new NetStream(nc);
ns.client = {};
ns.client.onMetaData = ns_onMetaData;
ns.client.onCuePoint = ns_onCuePoint;
ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv");
 
video.attachNetStream(ns);
 
function ns_onMetaData(item:Object):void {
    trace("metaData");
    // Resize video instance.
    video.width = item.width;
    video.height = item.height;
    // Center video instance on Stage.
    video.x = (stage.stageWidth - video.width) / 2;
    video.y = (stage.stageHeight - video.height) / 2;
}
 
function ns_onCuePoint(item:Object):void {
    trace("cuePoint");
    trace(item.name + "\t" + item.time);
}

{ 8 comments… read them below or add one }

1 christopher hill 02.04.09 at 12:56 pm

perfect. thanks! just what i was looking 4.

2 pablo 02.07.09 at 11:51 am

gr8 ! thanks

3 Geni 02.17.09 at 7:04 am

Thank you! it seems that the live docs aren’t too clear as for the onMetaData event regards

4 Consuelo Ore 03.23.09 at 12:25 pm

Hi,

This way to trace the cuePoints works perfect. Now I am trying to play another video after the cuePoint on the first video is reached.

Any ideas?

5 bobin 10.08.09 at 7:10 pm

can u tell me how will give a specified width and height?

thanks in advance

6 Eric 12.23.09 at 4:38 am

I’ve been looking for this the entire morning. Thnx so much!

7 Eric 12.23.09 at 6:30 am

I also found this for anti-aliasing the video: http://julian.empiregn.com/2006/1/30/Flash-Video-FLV-AntiAliasing

8 lucca 12.29.09 at 11:50 am

is there a way of inserting the video in a specific layer below the graphics i have?
thanks!

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Previous post:

Next post: