Question

I need to dynamically create a Video object in ActionScript 2 and add it to a movie clip. In AS3 I just do this:

var videoViewComp:UIComponent; // created elsewhere    

videoView = new Video();
videoView.width = 400;
videoView.height = 400;
this.videoViewComp.addChild(videoView);

Unfortunately, I can't figure out how to accomplish this in AS2. Video isn't a child of MovieClip, so attachMovie() doesn't seem to be getting me anything. I don't see any equivalent to AS3's UIComponent.addChild() method either.

Is there any way to dynamically create a Video object in AS2 that actually shows up on the stage?


I potentially need multiple videos at a time though. Is it possible to duplicate that video object?

I think I have another solution working. It's not optimal, but it fits with some of the things I have to do for other components so it's not too out of place in the project. Once I get it figured out I'll post what I did here.

Was it helpful?

Solution

Ok, I've got something working.

First, I created a new Library symbol and called it "VideoWrapper". I then added a single Video object to that with an ID of "video".

Now, any time I need to dynamically add a Video to my state I can use MovieClip.attachMovie() to add a new copy of the Video object.

To make things easier I wrote a VideoWrapper class that exposes basic UI element handling (setPosition(), setSize(), etc). So when dealing with the Video in regular UI layout code I just use those methods so it looks just like all my other UI elements. When dealing with the video I just access the "video" member of the class.

My actual implementation is a bit more complicated, but that's the basics of how I got things working. I have a test app that's playing 2 videos, one from the local camera and one streaming from FMS, and it's working great.

OTHER TIPS

To send you the ends of a line which is a tag, I use HTML Symbol Entities from w3schools

An example, taken from a project would be as follows:

< asset path="library\video.swf" />

The line above shows that there is a directory called library which contains the file video.swf

Besides, there is the file video.xml in the directory library. That file contains the lines

<xml version="1.0" encoding="utf-8" >
<movie version="7">    
    <frame>
        <library>
            <clip id="VideoDisplay">
                <frame>
                    <video id="VideoSurface" width="160" height="120" />
                    <place id="VideoSurface" name="video" />
                </frame>
            </clip>
        </library>
    </frame>
</movie>

Long ago my son Alex downloaded the code of VideoDisplay class and the directory library from Internet

I have iproved the code of the class VideoDisplay.

by writting 2 members

 public function pos():Number
{
    return ns.time;
}

     public function close():Void
{
    return ns.close();
}

The program I have created is more than an explorer and presenter of .flv files

It also is an explorer and presenter of the chosen fragments of each .flv file

Now the code of VideoDisplay class is:

class util.VideoDisplay
{
    //{ PUBLIC MEMBERS


    /**
    * Create a new video display surface
    */

    function VideoDisplay(targetURI:String, parent:MovieClip, name:String, depth:Number, initObj)

    {
        display = parent.attachMovie("VideoDisplay", name, depth, initObj);

        // create video stream
        nc = new NetConnection();
        nc.connect(targetURI);
        ns = new NetStream(nc);

        // attach the video stream to the video object
        display.video.attachVideo(ns);
    }

    /**
    * Video surface dimensions
    */
    function setSize(width:Number, heigth:Number):Void
    {
        display.video._width = width;
        display.video._height = heigth;
    }
    /**
    * Video clip position
    */
    function setLocation(x:Number, y:Number):Void
    {
        display._x = x;
        display._y = y;
    }

    /**
    * Start streaming
    * @param    url FLV file
    * @param    bufferTime  Buffer size (optional) 
    */
    public function play(url:String, bufferTime:Number):Void
    {
        if (bufferTime != undefined) ns.setBufferTime(bufferTime);
        ns.play(url);
    }
    /**
    * Pause streaming
    */
    public function pause():Void
    {
        ns.pause();
    }
    /**
    * Seek position in video
    */
    public function seek(offset:Number):Void
    {
        ns.seek(offset);
    }   

    /**
    * Get position in video
    */

     public function pos():Number
    {
        return ns.time;
    }

         public function close():Void
    {
        return ns.close();
    }

    //}

    //{ PRIVATE MEMBERS 

    private var display:MovieClip;
    private var nc:NetConnection;
    private var ns:NetStream;

    //}
}

I recommend you create a single instance of the Video object, leave it invisible (i.e., videoview.visible = false), and load the clip when you need it, displaying it at the appropriate time. You can also use swapDepth() if it becomes necessary.

Video handling in AS2 is not the best thing ever. Rest assured you'll run into a lot of little problems (looping without gaps, etc).

your approach is what i usually do because other option is to include the UIcomponent mediaDisplay into library and then attach that component using attachMovie but i found mediaDisplay i little buggy so i prefer to use the primitive video instance .

I hope that the code below will be very useful for you:

import UTIL.MEDIA.MEDIAInstances

class Main

{
    static function main() {

        var MEDIAInstancesInstance :MEDIAInstances  = new MEDIAInstances (); 

        _root.Video_Display.play ("IsothermalCompression.flv", 0);

        _root.VideoDisplayMC.onPress = function() { 

        _root.Video_Display.seek (0);        

        } // _root.displayMC.onPress = function() {

    } // static function main() 

} // class Main 

// 

import UTIL.MEDIA.VideoDisplay  

class UTIL.MEDIA.MEDIAInstances             

    {  

    function MEDIAInstances() 

    {

    //                                            depth  
    _root.createEmptyMovieClip ("VideoDisplayMC", 500);   
    //
    var Video_Display:VideoDisplay 
    = 
    new VideoDisplay(_root.VideoDisplayMC, "Video_Display", 1); 

    Video_Display.setLocation(400, 0); Video_Display.setSize (320, 240);      
    //    
    _root.Video_Display = Video_Display;  _root.VideoDisplayMC._alpha = 75;      

    } // MEDIAInstances()

} // class UTIL.MEDIA.MEDIAInstances

//

class UTIL.MEDIA.VideoDisplay

{
    private var display:MovieClip, nc:NetConnection, ns:NetStream;

    function VideoDisplay(parent:MovieClip, name:String, depth:Number)

    {
        display = parent.attachMovie("VideoDisplay", name, depth);

        nc = new NetConnection(); nc.connect(null); ns = new NetStream(nc);

        display.video.attachVideo(ns);
    }
    function setSize(width:Number, heigth:Number):Void

    { display.video._width = width; display.video._height = heigth;}

    function setLocation(x:Number, y:Number):Void { display._x = x; display._y = y;}

    public function play(url:String, bufferTime:Number):Void
    {
        if (bufferTime != undefined) ns.setBufferTime(bufferTime); ns.play(url);
    }
    //
    public function pause():Void { ns.pause();}
    //
    public function seek(offset:Number):Void { ns.seek(offset); }

} // UTIL.MEDIA.VideoDisplay
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top