as3多个视频同时播放效率问题,目前,只做了两种方式的测试。

第一种,用bitmap。用ENTER_FRAME,驱动,对视频进行draw,然后将draw的bitmapData给建立的多个bitmap。

第二种,就是笨笨的做多个nc,ns,video,让其播放,添加到显示列表中。

添加了8个视频,两种方式进行对比,发现第二种方式所占用的cpu是第一种的三倍,占用的内存约是第一种的两倍。

贴出code:

文档类:

package com.vini123
{
    import com.vini123.media.VideoPlayer;
    import com.vini123.global.GlobalEvent;
    import com.vini123.global.GlobalEventDis;

    import flash.display.Sprite;
    import flash.display.Bitmap;

    public class Main extends Sprite
    {
        private var _videoPlayer:VideoPlayer;
        private var _bitmapList:Vector.<Bitmap> = new Vector.<Bitmap>();
        private var _videoList:Vector.<VideoPlayer> = new Vector.<VideoPlayer>();
        private var _videoNum:int = 8;
        private var _type:int = 0;

        public function Main():void
        {
            Init();
        }

        private function Init():void
        {
            var i:int = 0;
            if (_type ==0)
            {
                for (i = 0; i < _videoNum; i++)
                {
                    _bitmapList.push(new Bitmap());
                    addChild(_bitmapList[i]);
                }
                _videoPlayer = new VideoPlayer("越单纯越幸福.FLV",{width:320,height:240,type:0});
                //addChild(_videoPlayer);
                GlobalEventDis.instance.addEventListener(GlobalEvent.BEGIN_SCREEN,beginScreenHandler);
            }
            else
            {
                for (i = 0; i < _videoNum; i++)
                {
                    _videoList.push(new VideoPlayer("越单纯越幸福.FLV",{width:320,height:240,type:1}));
                    addChild(_videoList[i]);
                    if (i/4 <1)
                    {
                        _videoList[i].x = _videoList[i].width * i;
                        _videoList[i].y = 0;
                    }
                    else
                    {
                        _videoList[i].x = _videoList[i].width * (i % 4);
                        _videoList[i].y = _videoList[i].height;
                    }
                    
                    if(i !=0)
                    {
                        _videoList[i].volume = 0;
                    }
                }
            }
        }

        private function beginScreenHandler(e:GlobalEvent):void
        {
            for (var i:int =0; i < _videoNum; i++)
            {
                _bitmapList[i].bitmapData = e.data;
                if (i/4 <1)
                {
                    _bitmapList[i].x = _bitmapList[i].width * i;
                    _bitmapList[i].y = 0;
                }
                else
                {
                    _bitmapList[i].x = _bitmapList[i].width * (i % 4);
                    _bitmapList[i].y = _bitmapList[i].height;
                }
            }
        }
    }
}

 

VideoPlayer.as

package com.vini123.media
{
    import com.vini123.global.GlobalEvent;
    import com.vini123.global.GlobalEventDis;

    import flash.display.Sprite;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.media.Video;
    import flash.display.BitmapData;
    import flash.events.Event;
    import flash.media.SoundTransform;


    public class VideoPlayer extends Sprite
    {
        private var _nc:NetConnection;
        private var _ns:NetStream;
        private var _video:Video;

        private var _url:String;
        private var _obj:Object;
        private var _bitmapData:BitmapData;

        public function VideoPlayer(_url:String,_obj:Object):void
        {
            this._url = _url;
            this._obj = _obj;
            play();
        }

        private function play():void
        {
            _nc = new NetConnection();
            _nc.connect(null);
            _ns = new NetStream(_nc);
            _ns.client = this;
            _ns.play(_url);

            _video = new Video();
            _video.width = _obj.width;
            _video.height = _obj.height;
            _video.attachNetStream(_ns);
        
            if(_obj.type == 0)
            {
                _bitmapData = new BitmapData(_obj.width,_obj.height,true,0);
                hasEnter();
            }
            else
            {
                addChild(_video);
            }

        }

        public function set volume(value:Number):void
        {
            var _soundTransform:SoundTransform = new SoundTransform();
            _soundTransform.volume = value;
            _ns.soundTransform = _soundTransform;
        }
        
        private function hasEnter():void
        {
            addEventListener(Event.ENTER_FRAME, enterHandler);
        }

        private function enterHandler(e:Event):void
        {
            if (_video)
            {
                if (_bitmapData)
                {
                    //_bitmapData.dispose();
                }
                _bitmapData.draw(_video);
                GlobalEventDis.instance.dispatchEvent(new GlobalEvent(GlobalEvent.BEGIN_SCREEN,_bitmapData));
            }
        }
        
        public function onMetaData(code:Object):void
        {
            
        }
    }
}

GlobalEvent.as

package com.vini123.global
{
    import flash.events.Event;
    import flash.display.BitmapData;

    public class GlobalEvent extends Event
    {
        public static const BEGIN_SCREEN:String = "begin screen";
        private var _data:BitmapData;

        public function GlobalEvent(type:String , _data:BitmapData):void
        {
            super(type);
            this._data = _data;
        }

        public function get data():BitmapData
        {
            return _data;
        }
    }
}

 

GlobalEventDis.as

package com.vini123.global
{
    import flash.events.EventDispatcher;
    import flash.events.IEventDispatcher;


    public class GlobalEventDis extends EventDispatcher
    {
        private static var _instance:GlobalEventDis;

        public function GlobalEventDis(target:IEventDispatcher = null):void
        {
            super(target);
        }

        public static function get instance():GlobalEventDis
        {
            if (! _instance)
            {
                _instance = new GlobalEventDis();
            }
            return _instance;
        }
    }
}