2

我在 AS3 中有用于视频连接的标准 NetStream:

编码:

public class VideoInit extends MovieClip
{
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.events.NetStatusEvent;


    public var video:Video;
    public var nc:NetConnection;
    public var ns:NetStream;
    public var meta:Object;
    public var _duration; //  <----- the variable



    public function VideoInit()
    {
        video = new Video(480,270); 
        addChild(video);
        nc = new NetConnection();
        nc.connect(null);
        ns = new NetStream(nc);

        meta = new Object();

        ns.client = meta;
        video.attachNetStream(ns);
        ns.play("video.f4v"); 


        meta = new Object();

        meta.onMetaData = function(meta:Object) // <---- the method
        {
            _duration = meta.duration

        }
        ns.client = meta;
     }

现在,每次执行 onMetaData 方法时,变量 _duration 都会存储该值,但在调试器步骤离开该方法之后,_duration 值会被设置回unindentified。我尝试通过推送值来解决 Array 的问题,但同样的事情发生了,Array 重置。有什么问题吗?

编辑:

一旦 onMetaData 完成,任何存储持续时间值的对象都会自行销毁。我如何实现它并不重要。

4

1 回答 1

3

您发布的代码无法编译,即使编译,也可能导致其他问题。

建议:放弃对象,改为设置ns.client = this;

这有效:

package
{
    import flash.events.Event;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.events.NetStatusEvent;

    public class VideoInit extends Sprite
    {
        private var video:Video;
        private var nc:NetConnection;
        private var ns:NetStream;
        private var _duration:Number; //  <----- the variable

        public function VideoInit()
        {
            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }

        private function onAddedToStage(e:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
            video = new Video(480,270); 
            addChild(video);
            nc = new NetConnection();
            nc.connect(null);

            ns = new NetStream(nc);
            ns.client = this;// meta;

            video.attachNetStream(ns);
            ns.play("video.f4v");
            stage.addEventListener(MouseEvent.MOUSE_DOWN, testTheValueOfDuration);
         }

         public function onMetaData(e:Object):void
         {
            _duration = e.duration
            trace('_duration:', _duration);

            testTheValueOfDuration();
         }

         private function testTheValueOfDuration(e:MouseEvent = null):void
         {
            trace('Seriously, what is the value of _duration?', _duration);
         }
    }
}
于 2012-11-11T04:09:07.423 回答