BIT-101 [2003-2017]

Quick and Dirty FLV Player


I don’t do a lot of work with video, and I was under the impression that you needed the Media Components in Flash MX 2004 pro to dynamically load and play an FLV file unless you were using FlashCom server.

But I was doing an app that needs to have very basic FLV playback capability, and I needed it to be lightweight, and preferably not UIComponent based. So I dug into the mx class files and saw how they did it and was able to recreate it very simply. I then searched for “FLV” in the Flash help and found almost the exact same code I had just written.

Basically, you trick Flash into thinking it’s getting the FLV from the com server, by setting up a dummy NetConnection object and a NetStream object connected to that. Then you use the new NetStream object to play your FLV.

First go to your library, choose “New Video” from the menu. Drag that new video to stage and name the instance “vid”. Then add this code to frame one:

var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
vid.attachVideo(ns);
ns.play("some.flv");

Voila, streaming FLV! In a swf that comes in at less than 1k!!

From there you can add some controls if you need them. Just some buttons to start and stop, maybe, or more if you want.

Of course, the Media components are very nice, but you are starting off at a minimum of about 55k just for a MediaDisplay. Use a MediaPlayback and you’re up to at least a 70k swf. If you’re on a kilobyte diet, or you just want some more basic customization than the components allow, there’s your solution.

« Previous Post
Next Post »