How can I load a FLV file into a MovieClip (lets call the instance "flvPlaceHolder") and start playing that FLV file.. using ActionScript 3?
views:
5037answers:
4Not explicitly answering your question, but there are a number of open source FLV players in the wild. I'd approach the problem by grabbing one of those and seeing how they handle playing video.
FPlayer would be an excellent starting point. Here is the class that is doing the work. It is fairly straight forward, but using a project like this would probably save you some time.
This snippet should do the trick in an extremely bare bones fashion:
var vid:Video = new Video(320, 240);
addChild(vid);
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
vid.attachNetStream(ns);
var listener:Object = new Object();
listener.onMetaData = function(evt:Object):void {};
ns.client = listener;
ns.play("externalVideo.flv");
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); }
As subha pointed out, its done using the NetStream class... however, that class in particular is quite hard to work with, and very uncoherent with the rest of the language...
I would strongly advice you to use some library, class or component to wrap it up. The FLVPlayback component in Flash (without any skins), in contrast with all the other built-in components in Flash, is really straight forward and very easy to use ;)
to do this locally - cut and paste the following code in the first frame of your flash file. Of course change the name at the end.
stage.displayState = StageDisplayState.FULL_SCREEN;
var connection:NetConnection = new NetConnection(); var stream:NetStream; var video:Video = new Video(1280,960); var metaObj:Object = new Object();
function onMetaData(data:Object):void {
}
connection.connect(null); stream = new NetStream(connection); stream.client = metaObj; metaObj.onMetaData = onMetaData; video.attachNetStream(stream); addChild(video); stream.play("name_of_flv.flv"); video.x = 0; video.y = 0;