ActionScript 3 视频循环

Actionscript 3 Video loop

我想在 Flash 游戏中嵌入视频并循环播放。有办法吗?正如我的代码或更可能的 .seek 或 .resume 没有任何效果?我使用了错误的命令还是?

[Embed(source = "fast.flv", mimeType = "application/octet-stream")]
public var bytes:Class;
public var vidNS:NetStream 
public var video:Video = new Video(1280, 720);
public var ns:NetStream;    


public function Main() {
Doit();
}

    private function Doit():void{

    addChild(video);

        var vidNC:NetConnection = new NetConnection(); vidNC.connect(null);
        vidNS = new NetStream(vidNC);

        var metaListener :Object = new Object(); metaListener = { onMetaData: process_Metadata };
        vidNS.client = metaListener;
        vidNS.addEventListener(NetStatusEvent.NET_STATUS, videoStatusHandler);      

        vidNS.play(null);

    var file:ByteArray = new bytes();
    vidNS.appendBytes(file);
    video.attachNetStream(vidNS);

}   
        function process_Metadata (in_Data :Object):void
        {
            trace("duration is : " + in_Data.duration );
        }

    function videoStatusHandler (event:NetStatusEvent):void 
    {
        if (event.info.code == "NetStream.Buffer.Empty") 
        {
            trace('loop')
            vidNS.seek(0); vidNS.resume();
        }
    }   

您可以通过简单地将字节重新馈送到解码器 (NetStream) 来进行循环

有可能你还需要涉及选项RESET_BEGIN:

yourNS.appendBytesAction( NetStreamAppendBytesAction.RESET_BEGIN );

尝试像下面这样设置您的代码:

[Embed(source = "fast.flv", mimeType = "application/octet-stream")]
public var bytes:Class;

public var vidNS:NetStream;
public var video:Video = new Video(1280, 720);
public var ns:NetStream;

//# declare these vars outside of a function for global access
public var file:ByteArray = new bytes(); 
public var vidNC:NetConnection;
public var metaListener :Object;

public function Main() 
{
    Doit();
}

private function Doit():void
{
    addChild(video);

    vidNC = new NetConnection(); vidNC.connect(null);
    vidNS = new NetStream(vidNC);

    metaListener new Object(); metaListener = { onMetaData: process_Metadata };
    vidNS.client = metaListener;
    vidNS.addEventListener(NetStatusEvent.NET_STATUS, videoStatusHandler);      

    vidNS.play(null);

    //var file:ByteArray = new bytes();
    vidNS.appendBytes(file); //# feed video bytes to decoder
    video.attachNetStream(vidNS);

}   

function process_Metadata (in_Data :Object):void
{
    trace("duration is : " + in_Data.duration );
}

function videoStatusHandler (event:NetStatusEvent):void 
{
    if (event.info.code == "NetStream.Buffer.Empty") 
    {
        trace('now doing: looping...')

        //# there is no timeline ".seek" in appendBytes mode, use ".seek" to clear the buffer for a new re-feed

        vidNS.seek(0); //clears the current buffer (video data is removed)
        vidNS.appendBytesAction( NetStreamAppendBytesAction.RESET_BEGIN ); //re-set timestamps for decoder
        vidNS.appendBytes(file); //re-feed video bytes to decoder

    }
} 

所以感谢 VC。一个我最终得到了这个代码:

[Embed(source="1.flv", mimeType="application/octet-stream")]
public var bytes:Class;
public var vidNS:NetStream;
public var video:Video = new Video(1280, 720);
public var file:ByteArray = new bytes(); 
public var vidNC:NetConnection;
public var metaListener :Object;

public function Main() 
{
    Doit();
}

private function Doit():void
{
    addChild(video);

    vidNC = new NetConnection(); vidNC.connect(null);
    vidNS = new NetStream(vidNC);

    metaListener new Object(); metaListener = { onMetaData: process_Metadata };
    vidNS.client = metaListener;   

    vidNS.play(null);    
    vidNS.appendBytes(file); 
    video.attachNetStream(vidNS);    
} 

function process_Metadata (in_Data :Object):void
{
    trace("loop ");
            vidNS.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
            vidNS.appendBytes(file);
}

它工作正常,但我不确定这是否是正确的方法 ^^。