Friday, April 9, 2010

Video object with the NetConnection and NetStream classes

This class creates a Video object in an Adobe Flash interface that plays either of the following kinds of video: recorded Flash Video (FLV) files stored on a server

or locally, or live video captured from a user's computer. A Video object is a display object on the application's display list and represents the visual space in which the video runs in a user interface.

You can control various properties of Video objects. For example, you can move the Video object around on the Stage by using its x and y properties, you can change its size using its height  and width properties, and so on. To play a video stream, use attachCamera() or attachNetStream()  to attach the video to the Video object. Then, add the Video object to the display list using addChild().


The following example uses a Video object with the NetConnection and NetStream classes to

load and play an FLV file. To run this example, you need an FLV file whose name and location

match the variable passed to videoURL, in this case, an FLV file called test.flv that is in

the same directory as the SWF file.

package {
    import flash.display.Sprite;
    import flash.display.*;
    import flash.events.*;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;

    public class VideoManagerextends Sprite {
        var ns:NetStream;
        var vid:Video;
        var vdoUrl:String = "test.flv";
        public function VideoManager() {
            var nc:NetConnection=new NetConnection  ;
            nc.connect(null);
            ns=new NetStream(nc);
            vid=new Video();
            vid.width=1024;
            vid.height=768;
            this.addChild(vid);
            vid.attachNetStream(ns);
            ns.play(vdoUrl);
            ns.addEventListener(NetStatusEvent.NET_STATUS,netstat);
            var netClient:Object=new Object  ;
            netClient.onMetaData=function(meta:Object){
            trace("duration:   "+meta.duration);
            };
            ns.client=netClient;
            // function for checking flv complete or not
            function netstat(stats:NetStatusEvent) {
                trace("info:  "+stats.info.code);
                if (stats.info.code=="NetStream.Buffer.Flush") {
                    ns.pause();
                    this.removeChild(vid);
                }
            }// end of the netstat function
        }// end of the function testComponentFlv
    }
}

1. Paste the class code into a new AS file and give the file the same name as the primary class  "VideoManager.as".
2. Create and save a new empty FLA file in the same directory as the AS file.
3. In the Properties tab of the Property inspector enter the class name of the primary class for the example in the Document class text box "VideoManager".
4. Save your changes to the FLA file.
5. Test the movie using the Control > Test Movie menu option.
6. Make sure test.flv is in the same directory as the SWF file.

1 comment:

  1. nice tutorial, Thanks i needed that! This information was very helpful to me. :)

    ReplyDelete