Playing back FLVs with ActionScript 3.0

OK, this isn’t a huge difference between ActionScript 2.0 and ActionScript 3.0, but it is good to know anyways…

In previous versions of ActionScript, in order to play back and FLV (without using a component) you’d need to create a Video object in the library, add it to the Stage, and give it an instance name, as seen in the following example:

// AS2
// requires: Video symbol on Stage with instance name of "myVideo".
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
myVideo.attachVideo(ns);
ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv");

In ActionScript 3.0, you can now create a new Video object using the Video() constructor, and adding it to the display list using the addChild() method, as seen in the following example:

// AS3
var myVideo:Video = new Video();
addChild(myVideo);
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
myVideo.attachNetStream(ns);
ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv");

Where it starts to get a bit more interesting, is when you want to work with video cue points and meta data. ActionScript 2.0 used the onCuePoint and onMetaData event handlers to process cue points and meta data, as shown in the following example:

// AS2
// requires: Video symbol on Stage with instance name of "myVideo".
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.onCuePoint = function(infoObject:Object):Void {
trace("cuePoint");
}
ns.onMetaData = function (infoObject:Object):Void {
trace("metaData");
}
myVideo.attachVideo(ns);
ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv");

In ActionScript 3.0, you have a few more options for handling the information. One of the simplest methods is to create a simple Object which processes the onCuePoint and onMetaData event handlers in ActionScript 3.0. You can see an example of this in the following code:

// AS3
var customClient:Object = new Object();
customClient.onCuePoint = cuePointHandler;
customClient.onMetaData = metaDataHandler;

var myVideo:Video = new Video();
addChild(myVideo);

var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = customClient;
myVideo.attachNetStream(ns);
ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv");

function cuePointHandler(infoObject:Object):void {
trace("cuePoint");
}
function metaDataHandler(infoObject:Object):void {
trace("metaData");
}

This code begins by creating a new Object instance named customClient which defines listeners for the onCuePoint and onMetaData event handlers. The cuePointHandler and metaDataHandler variables point to user-defined functions which are defined later in the code. Later, the client property on the NetStream instance is set to the customClient object.

Another way of handling the cue points and meta data is to create a new class which you can use with the client property. The code is roughly similar to the previous example:

// AS3
var myVideo:Video = new Video();
addChild(myVideo);

var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = new CustomClient();
myVideo.attachNetStream(ns);
ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv");

This time, instead of defining an object to handle the onCuePoint and onMetaData event handlers, the code is moved into an external file named “CustomClient”, which contains the following code:

package {
public class CustomClient {
public function onCuePoint(infoObject:Object):void {
trace("onCuePoint");
}
public function onMetaData(infoObject:Object):void {
trace("onMetaData");
}
}
}

On the other hand, if you wanted to ignore the cue point and meta data information from an FLV altogether, you could use either one of the following snippets:

// AS3
var myVideo:Video = new Video();
addChild(myVideo);

var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = new Object();
myVideo.attachNetStream(ns);
ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv");

The previous example sets the client property to a new Object instance, which doesn’t have any event handlers for the onCuePoint and onMetaData callbacks. As an alternative, you could also set up an event listener for the AsyncErrorEvent.ASYNC_ERROR event which does nothing, as seen in the following code:

// AS3
var myVideo:Video = new Video();
addChild(myVideo);

var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorEventHandler);
myVideo.attachNetStream(ns);
ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv");

function asyncErrorEventHandler(event:AsyncErrorEvent):void {
// ignore
}