[ActionScript 3.0] AS3.0 让一个视频无缝循环播放的一个偏方

一个视频要循环播放,通常的办法都是播放完后再重新播放,但是不可避免的播放结束和重新开始播放这个时间点上会有停顿,如何解决这个问题,说个偏方吧!

package com.views
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.utils.setTimeout;
    
    /**
     * @author Frost.Yen
     * @email 871979853@qq.com
     * @build 2016-8-9 上午10:53:13
     */
    public class VideoView extends Sprite
    {
        private var _video1:VideoDisplay;
        private var _video2:VideoDisplay;
        private var _currVideo:VideoDisplay;
        private var _duration:Number;
        private var _b:Boolean;
        public function VideoView(url:String)
        {
            _video1 = new VideoDisplay(url,true);
            _video2 = new VideoDisplay(url);
            _video1.onMetaData = onMetaData;
            _video2.visible = false;
            _currVideo = _video1;
            this.addChild(_video1);
            this.addChild(_video2);
            this.addEventListener(Event.ENTER_FRAME,onCheck);
        }
        private function onMetaData():void
        {
            _duration = _video1.duration;
        }
        private function onCheck(e:Event):void
        {
            //trace("vs.time= " + _currVideo.time+ " duration= " + _duration );
            if (_currVideo.time > 0 && _duration > 0)
            {
                
                if (_duration-_currVideo.time <=0.1 )
                {
                    
                    this.removeEventListener(Event.ENTER_FRAME,onCheck);
                    setTimeout(add,2000);
                    if(_video1.visible){
                        _video1.visible = false;
                        _video2.visible = true;
                        _video1.seek(0);
                        _video1.pause();
                        _video2.play();
                        _currVideo = _video2;
                    }else{
                        _video2.visible = false;
                        _video1.visible = true;
                        _video2.seek(0);
                        _video2.pause();
                        _video1.play();
                        _currVideo = _video1;
                    }
                    
                    //_ns.seek(0);//跳转到0秒处播放。
                }
            }
        }
        private function add():void
        {
            this.addEventListener(Event.ENTER_FRAME,onCheck);
        }
    }
}
import flash.display.Sprite;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;

class VideoDisplay extends Sprite
{
    private var _video:Video;
    private var _ns:NetStream;
    private var _duration:Number;
    public var onMetaData:Function;
    public function VideoDisplay(url:String,auto:Boolean=false)
    {
        var nc:NetConnection = new NetConnection();
        nc.connect(null);
        _ns = new NetStream(nc);    
        var client:Object = new Object( );
        client.onMetaData = function(data:Object):void {
            //trace(data.duration);
            _duration = data.duration;
            if(onMetaData!=null){
                onMetaData();
            }
            
        };
        _ns.client = client;
        _video = new Video(1920,1080);
        _video.attachNetStream(_ns);
        _ns.play(url);
        if(!auto){
            _ns.seek(0);
            _ns.pause();
        }
        this.addChild(_video);
    }    
    public function play():void
    {
        _ns.resume();
    }
    public function pause():void
    {
        _ns.pause();
    }
    public function seek(offset:Number):void
    {
        _ns.seek(offset);
    }
    public function get duration():Number
    {
        return _duration;
    }
    public function get time():Number
    {
        return _ns.time;
    }
}

 

posted on 2016-08-29 14:56  晏过留痕  阅读(5611)  评论(0编辑  收藏