views:

1870

answers:

5

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?

A: 

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).

Pedro
A: 

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.

Herms
A: 

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.

Herms
A: 

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 .

A: 

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
Tonio FERENER-VARI