Loading video files

Loading videos using the NetStream and NetConnection classes is a multistep process:

  1. Create a NetConnection object. If you are connecting to a local video file or one that is not using a server such as Adobe's Flash Media Server 2, pass null to the connect() method to play the video files from either an HTTP address or a local drive. If you are connecting to a server, set the parameter to the URI of the application that contains the video file on the server.

    var nc:NetConnection = new NetConnection(); 
    nc.connect(null);
  2. Create a NetStream object which takes a NetConnection object as a parameter and specify the video file that you want to load. The following snippet connects a NetStream object to the specified NetConnection instance and loads a video file named video.mp4 in the same directory as the SWF file:

    var ns:NetStream = new NetStream(nc); 
    ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler); 
    ns.play("video.mp4"); 
    function asyncErrorHandler(event:AsyncErrorEvent):void 
    { 
        // ignore error 
    }
  3. Create a new Video object and attach the previously created NetStream object using the Video class’s attachNetStream() method. Then you can add the video object to the display list using the addChild() method, as seen in the following snippet:

    var vid:Video = new Video(); 
    vid.attachNetStream(ns); 
    addChild(vid);

As Flash Player executes this code it attempts to load the video.mp4 video file from the same directory as your SWF file.