如何在AS2中动态创建Video对象并将其添加到MovieClip?

时间:2022-05-19 07:22:26

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

我需要在ActionScript 2中动态创建一个Video对象并将其添加到影片剪辑中。在AS3中我只是这样做:

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.

不幸的是,我无法弄清楚如何在AS2中实现这一目标。视频不是MovieClip的子节点,所以attachMovie()似乎没有给我任何东西。我也没有看到AS3的UIComponent.addChild()方法的任何等价物。

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

有没有办法在AS2中动态创建一个实际出现在舞台上的Video对象?


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.

我想我还有另一个解决方案。它不是最优的,但它适合我必须为其他组件做的一些事情,所以它在项目中并不是太不合适。一旦我弄明白,我会发布我在这里做的事情。

5 个解决方案

#1


2  

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

首先,我创建了一个新的Library符号,并将其命名为“VideoWrapper”。然后我添加了一个视频对象,其ID为“视频”。

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.

现在,每当我需要动态添加视频到我的状态时,我可以使用MovieClip.attachMovie()添加Video对象的新副本。

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.

为了方便起见,我写了一个VideoWrapper类,它暴露了基本的UI元素处理(setPosition(),setSize()等)。因此,当在常规UI布局代码中处理视频时,我只使用这些方法,因此它看起来就像我所有的其他UI元素。在处理视频时,我只是访问该类的“视频”成员。

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.

我的实际实现有点复杂,但这是我如何工作的基础知识。我有一个测试应用程序正在播放2个视频,一个来自本地摄像头,另一个来自FMS,它运行良好。

#2


1  

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

为了向您发送作为标记的行的末尾,我使用来自w3schools的HTML符号实体

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

上面的一行显示有一个名为library的目录,其中包含文件video.swf

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

此外,目录库中还有文件video.xml。该文件包含行

<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

很久以前,我的儿子Alex从Internet下载了VideoDisplay类的代码和目录库

I have iproved the code of the class VideoDisplay.

我已经改进了VideoDisplay类的代码。

by writting 2 members

通过写2个成员

 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

我创建的程序不仅仅是.flv文件的资源管理器和演示者

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

它也是每个.flv文件所选片段的资源管理器和演示者

Now the code of VideoDisplay class is:

现在VideoDisplay类的代码是:

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;

    //}
}

#3


0  

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对象的实例,让它不可见(即videoview.visible = false),并在需要时加载剪辑,并在适当的时候显示它。如果有必要,您还可以使用swapDepth()。

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

AS2中的视频处理并不是最好的。请放心,你会遇到很多小问题(无间隙循环等)。

#4


0  

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 .

你的方法是我通常做的,因为其他选项是将UI组件mediaDisplay包含到库中,然后使用attachMovie附加该组件,但我发现mediaDisplay我有点小马车,所以我更喜欢使用原始视频实例。

#5


0  

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

#1


2  

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

首先,我创建了一个新的Library符号,并将其命名为“VideoWrapper”。然后我添加了一个视频对象,其ID为“视频”。

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.

现在,每当我需要动态添加视频到我的状态时,我可以使用MovieClip.attachMovie()添加Video对象的新副本。

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.

为了方便起见,我写了一个VideoWrapper类,它暴露了基本的UI元素处理(setPosition(),setSize()等)。因此,当在常规UI布局代码中处理视频时,我只使用这些方法,因此它看起来就像我所有的其他UI元素。在处理视频时,我只是访问该类的“视频”成员。

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.

我的实际实现有点复杂,但这是我如何工作的基础知识。我有一个测试应用程序正在播放2个视频,一个来自本地摄像头,另一个来自FMS,它运行良好。

#2


1  

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

为了向您发送作为标记的行的末尾,我使用来自w3schools的HTML符号实体

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

上面的一行显示有一个名为library的目录,其中包含文件video.swf

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

此外,目录库中还有文件video.xml。该文件包含行

<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

很久以前,我的儿子Alex从Internet下载了VideoDisplay类的代码和目录库

I have iproved the code of the class VideoDisplay.

我已经改进了VideoDisplay类的代码。

by writting 2 members

通过写2个成员

 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

我创建的程序不仅仅是.flv文件的资源管理器和演示者

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

它也是每个.flv文件所选片段的资源管理器和演示者

Now the code of VideoDisplay class is:

现在VideoDisplay类的代码是:

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;

    //}
}

#3


0  

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对象的实例,让它不可见(即videoview.visible = false),并在需要时加载剪辑,并在适当的时候显示它。如果有必要,您还可以使用swapDepth()。

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

AS2中的视频处理并不是最好的。请放心,你会遇到很多小问题(无间隙循环等)。

#4


0  

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 .

你的方法是我通常做的,因为其他选项是将UI组件mediaDisplay包含到库中,然后使用attachMovie附加该组件,但我发现mediaDisplay我有点小马车,所以我更喜欢使用原始视频实例。

#5


0  

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