How to Load External Flash Video (FLV) Files (AS2)

ActionScript 2.0 Flash

Note:  This article has a companion piece, “How to Control Video (FLV) without a Component.”  An ActionScript 3.0 version of this article is located in a more recent entry of this blog.

It has been possible to load external video into a movie since Flash MX (aka Flash 6).  Loading FLV files, however, isn’t nearly as intuitive as loading other external media, such as SWFs, JPGs, MP3s, and the like.  The MovieClip.loadMovie() method, for example, loads an external SWF with a single line of code.  Not so for FLV.  So how is it done? 

Many people take the route of one of the Flash MX/Flash MX 2004 Media Components or the Flash 8 FLVPlayback Component.  These provide the convenience of drag-and-drop setup (just set the contentPath property in your Component Inspector panel) and UI controls for playing, pausing, adjusting volume, and more.  Certainly, nice features — but you may simply want a no-frills display, “Just give me the video.”  Besides, these Components only ship with the Professional editions of Flash and they do add to the SWF footprint.  The older Components weigh between 55KB and 68KB on their own.  The newer Component weighs less, but requires a Flash Player 8 SWF and still adds 33KB to your movie.

An answer, short and sweet

Let’s get to business, then.  Here’s a virtually 0KB way to add video to your SWFs, compatible back to Flash Player 6*.

  • Click on the Options menu of your Library panel (the square-ish icon in the upper right corner) and choose New Video….
  • Drag an instance of your new video object to the Stage.  Set its height and width as desired.
  • While the instance is selected, use the Property inspector to provide an instance name.  In this example, we’ll use videoPlayer.
  • Add the following ActionScript to a keyframe of the timeline that holds the video object.  If you like to be organized (and you do!), create a dedicated scripts layer, but any layer will suffice.
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
videoPlayer.attachVideo(ns);
ns.play("externalVideo.flv");

* Important Note:  According to the ActionScript Language Reference, the NetConnection and NetStream classes require Flash Player 7 or higher.  In my own experience, I find success publishing to a Flash Player 6 SWF with ActionScript 2.0.

How it works

NetConnection is designed to provide playback of streaming FLVs from a local drive or HTTP address.  By supplying the NetConnection.connect() method with a null parameter, we avoid dependence on Flash Media Server (formerly known as Flash Communication Server) in order to play video.

NetStream handles the video stream for NetConnection, and our videoPlayer instance cooperates with all of the above via the Video.attachVideo() method.  Finally, our NetStream instance loads the external FLV via the NetStream.play() method.

The desired video will begin playing before the file completely loads — when enough data have buffered — so this is a great way to load sizeable video content without significant download time.

Leave a Reply