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

<pre><code>var videoViewComp:UIComponent; // created elsewhere    

videoView = new Video();
videoView.width = 400;
videoView.height = 400;
this.videoViewComp.addChild(videoView);
</code></pre>

<p>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.</p>

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

<hr>

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

<p>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.</p>

[{'answer_id': 53254, 'author': 'Pedro', 'author_id': 5488, 'author_profile': 'https://Stackoverflow.com/users/5488', 'pm_score': 0, 'selected': False, 'text': "<p>I recommend you create a single instance of the Video object, leave it invisible (i.e., <code>videoview.visible = false</code>), and load the clip when you need it, displaying it at the appropriate time. You can also use <code>swapDepth()</code> if it becomes necessary.</p>\n\n<p>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).</p>\n"}, {'answer_id': 55141, 'author': 'Herms', 'author_id': 1409, 'author_profile': 'https://Stackoverflow.com/users/1409', 'pm_score': 2, 'selected': True, 'text': '<p>Ok, I\'ve got something working.</p>\n\n<p>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".</p>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n'}, {'answer_id': 99109, 'author': 'paranoio', 'author_id': 11124, 'author_profile': 'https://Stackoverflow.com/users/11124', 'pm_score': 0, 'selected': False, 'text': '<p>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 .</p>\n'}, {'answer_id': 3603369, 'author': 'Tonio FERENER-VARI', 'author_id': 435264, 'author_profile': 'https://Stackoverflow.com/users/435264', 'pm_score': 0, 'selected': False, 'text': '<p>I hope that the code below will be very useful for you:</p>\n\n<pre><code>import UTIL.MEDIA.MEDIAInstances\n\nclass Main\n\n{\n    static function main() {\n\n        var MEDIAInstancesInstance :MEDIAInstances  = new MEDIAInstances (); \n\n        _root.Video_Display.play ("IsothermalCompression.flv", 0);\n\n        _root.VideoDisplayMC.onPress = function() { \n\n        _root.Video_Display.seek (0);        \n\n        } // _root.displayMC.onPress = function() {\n\n    } // static function main() \n\n} // class Main \n\n// \n\nimport UTIL.MEDIA.VideoDisplay  \n\nclass UTIL.MEDIA.MEDIAInstances             \n\n    {  \n\n    function MEDIAInstances() \n\n    {\n\n    //                                            depth  \n    _root.createEmptyMovieClip ("VideoDisplayMC", 500);   \n    //\n    var Video_Display:VideoDisplay \n    = \n    new VideoDisplay(_root.VideoDisplayMC, "Video_Display", 1); \n\n    Video_Display.setLocation(400, 0); Video_Display.setSize (320, 240);      \n    //    \n    _root.Video_Display = Video_Display;  _root.VideoDisplayMC._alpha = 75;      \n\n    } // MEDIAInstances()\n\n} // class UTIL.MEDIA.MEDIAInstances\n\n//\n\nclass UTIL.MEDIA.VideoDisplay\n\n{\n    private var display:MovieClip, nc:NetConnection, ns:NetStream;\n\n    function VideoDisplay(parent:MovieClip, name:String, depth:Number)\n\n    {\n        display = parent.attachMovie("VideoDisplay", name, depth);\n\n        nc = new NetConnection(); nc.connect(null); ns = new NetStream(nc);\n\n        display.video.attachVideo(ns);\n    }\n    function setSize(width:Number, heigth:Number):Void\n\n    { display.video._width = width; display.video._height = heigth;}\n\n    function setLocation(x:Number, y:Number):Void { display._x = x; display._y = y;}\n\n    public function play(url:String, bufferTime:Number):Void\n    {\n        if (bufferTime != undefined) ns.setBufferTime(bufferTime); ns.play(url);\n    }\n    //\n    public function pause():Void { ns.pause();}\n    //\n    public function seek(offset:Number):Void { ns.seek(offset); }\n\n} // UTIL.MEDIA.VideoDisplay\n</code></pre>\n'}, {'answer_id': 4334511, 'author': 'Tonio FERENER-VARI', 'author_id': 527889, 'author_profile': 'https://Stackoverflow.com/users/527889', 'pm_score': 1, 'selected': False, 'text': '<p>To send you the ends of a line which is a tag, I use HTML Symbol Entities from <a href="http://www.w3schools.com/tags/ref_symbols.asp" rel="nofollow">w3schools</a></p>\n\n<p>An example, taken from a project would be as follows:</p>\n\n<pre><code>&lt; asset path="library\\video.swf" /&gt;\n</code></pre>\n\n<p>The line above shows that there is a directory called library which contains the file <code>video.swf</code> </p>\n\n<p>Besides, there is the file video.xml in the directory library. That file contains the lines</p>\n\n<pre><code>&lt;xml version="1.0" encoding="utf-8" &gt;\n&lt;movie version="7"&gt;    \n    &lt;frame&gt;\n        &lt;library&gt;\n            &lt;clip id="VideoDisplay"&gt;\n                &lt;frame&gt;\n                    &lt;video id="VideoSurface" width="160" height="120" /&gt;\n                    &lt;place id="VideoSurface" name="video" /&gt;\n                &lt;/frame&gt;\n            &lt;/clip&gt;\n        &lt;/library&gt;\n    &lt;/frame&gt;\n&lt;/movie&gt;\n</code></pre>\n\n<p>Long ago my son Alex downloaded the code of VideoDisplay class and the directory library from Internet</p>\n\n<p>I have iproved the code of the class VideoDisplay.</p>\n\n<p>by writting 2 members</p>\n\n<pre><code> public function pos():Number\n{\n    return ns.time;\n}\n\n     public function close():Void\n{\n    return ns.close();\n}\n</code></pre>\n\n<p>The program I have created \nis \nmore than an explorer and presenter of <code>.flv</code> files</p>\n\n<p>It also\nis an explorer and presenter of the chosen fragments of each <code>.flv</code> file </p>\n\n<p>Now the code of VideoDisplay class is:</p>\n\n<pre><code>class util.VideoDisplay\n{\n    //{ PUBLIC MEMBERS\n\n\n    /**\n    * Create a new video display surface\n    */\n\n    function VideoDisplay(targetURI:String, parent:MovieClip, name:String, depth:Number, initObj)\n\n    {\n        display = parent.attachMovie("VideoDisplay", name, depth, initObj);\n\n        // create video stream\n        nc = new NetConnection();\n        nc.connect(targetURI);\n        ns = new NetStream(nc);\n\n        // attach the video stream to the video object\n        display.video.attachVideo(ns);\n    }\n\n    /**\n    * Video surface dimensions\n    */\n    function setSize(width:Number, heigth:Number):Void\n    {\n        display.video._width = width;\n        display.video._height = heigth;\n    }\n    /**\n    * Video clip position\n    */\n    function setLocation(x:Number, y:Number):Void\n    {\n        display._x = x;\n        display._y = y;\n    }\n\n    /**\n    * Start streaming\n    * @param    url FLV file\n    * @param    bufferTime  Buffer size (optional) \n    */\n    public function play(url:String, bufferTime:Number):Void\n    {\n        if (bufferTime != undefined) ns.setBufferTime(bufferTime);\n        ns.play(url);\n    }\n    /**\n    * Pause streaming\n    */\n    public function pause():Void\n    {\n        ns.pause();\n    }\n    /**\n    * Seek position in video\n    */\n    public function seek(offset:Number):Void\n    {\n        ns.seek(offset);\n    }   \n\n    /**\n    * Get position in video\n    */\n\n     public function pos():Number\n    {\n        return ns.time;\n    }\n\n         public function close():Void\n    {\n        return ns.close();\n    }\n\n    //}\n\n    //{ PRIVATE MEMBERS \n\n    private var display:MovieClip;\n    private var nc:NetConnection;\n    private var ns:NetStream;\n\n    //}\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/52732', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1409/']