Flash/Flex学习笔记(37):不用系统组件(纯AS3)的视频播放器--只有8.82K
以前为了赶项目,利用系统组件制作过一款视频播放器(见Flash/Flex学习笔记(6):制作基于xml数据源的flv视频播放器),但是系统组件实在是太大了,最终生成的swf居然有103K,随着AS3的深入学习,昨天又弄了一个只用AS3的播放器,最终只有8.82K,呵呵,这肥减得那是相当厉害。
用到了上一篇(Flash/Flex学习笔记(35):自己动手实现一个滑块控件(JimmySilder))里自己写的的滑块控件,主要代码如下(关键是NetConnection与NetStream对象的使用):
import Jimmy.Event.ValueChangeEvent; var _autoPlay:Boolean=true;//是否自动播放 var _netConn:NetConnection; var _netStream:NetStream; var _duration:Number=0.0; var _client:Object; var _soundTransform:SoundTransform; var _volumn=0.6;//初始音量 var _flvUrl = "flv的地址"; function init():void { txtInfo.text = ""; txtInfo.visible = false; txtCurrent.text = "00:00"; txtTotal.text = "00:00"; silderVideo.Value = 0.0; videoMask.visible = false; txtPercent.text = ""; txtPercent.visible = false; _netConn = new NetConnection();//创建连接 _client = new Object(); _soundTransform = new SoundTransform(); //初始化[暂停]/[播放]按钮可见性 if (_autoPlay) { btnPause.visible=true; btnPlay.visible=false; } else { btnPause.visible=false; btnPlay.visible=true; } //初始化滑块按钮宽度 silderSound.ButtonWidth=35; silderVideo.ButtonWidth=9.5; silderSound.Value = _volumn; silderSound.ShowBarTri = true; silderSound.ShowBar = false; //开始连接并播放 _netConn.connect(null); _netStream=new NetStream(_netConn); _netStream.bufferTime = 5; wVideo.attachNetStream(_netStream); _netStream.play(_flvUrl); _soundTransform.volume=_volumn; _netStream.soundTransform=_soundTransform; _client.onMetaData = MetaDataHandler; _netStream.client=_client; //注册监听事件 _netStream.addEventListener(NetStatusEvent.NET_STATUS,NetStatusHandler); btnSound.addEventListener(MouseEvent.MOUSE_OVER,function(){ Mouse.cursor = MouseCursor.BUTTON}); btnSound.addEventListener(MouseEvent.MOUSE_OUT,function(){ Mouse.cursor = MouseCursor.AUTO}); btnSound.addEventListener(MouseEvent.MOUSE_DOWN,btnSoundMouseDown); btnPlay.addEventListener(MouseEvent.CLICK,btnPlayMouseClick); btnPause.addEventListener(MouseEvent.CLICK,btnPauseMouseClick); btnStop.addEventListener(MouseEvent.CLICK,btnStopMouseClick); silderSound.addEventListener(ValueChangeEvent.VALUE_CHANGE,VolumnChangedHandler); addEventListener(Event.ENTER_FRAME,EnterFrameHandler); silderVideo.addEventListener(MouseEvent.MOUSE_DOWN,silderVideoMouseDown); silderVideo.addEventListener(ValueChangeEvent.VALUE_CHANGE,VideoValueChangedHandler); } function NetStatusHandler(e:NetStatusEvent):void{ txtInfo.text = ""; videoMask.visible = txtPercent.visible = txtInfo.visible = false; trace(e.info.code); switch(e.info.code){ case "NetStream.Play.StreamNotFound": txtInfo.text = "错误:视频文件地址无法访问" break; case "NetStream.Play.Failed": txtInfo.text = "错误:视频播放失败" break; case "NetStream.Buffer.Empty": txtInfo.text = "影片缓冲中..."; break; case "NetStream.Play.Stop": //txtInfo.text = "播放已停止"; break; case "NetStream.Seek.InvalidTime": //txtInfo.text = "错误:无法跳到指定的时间点播放"; //trace(e.info.Error); //_netStream.pause(); //btnPlay.visible = true; //btnPause.visible = false; break; default: break; } if (txtInfo.text.length>0){ videoMask.visible = txtPercent.visible = txtInfo.visible = true; } } function btnSoundMouseDown(e:MouseEvent) { var i:uint=btnSound.currentFrame; if (i==1) { btnSound.gotoAndStop(2);//切换到静音 SetVolumn(0); } else { btnSound.gotoAndStop(1);//切换到有声音 SetVolumn(silderSound.Value); } } //设置音量 function SetVolumn(vol:Number):void { var _snd:SoundTransform = _netStream.soundTransform; _snd.volume = vol; _netStream.soundTransform = _snd; } //Play按钮点击事件 function btnPlayMouseClick(e:MouseEvent) { //切换按钮可用性 btnPause.visible=true; btnPlay.visible=false; //开始播放... _netStream.resume(); } //Pause按钮点击事件 function btnPauseMouseClick(e:MouseEvent) { //切换按钮可用性 btnPause.visible=false; btnPlay.visible=true; //暂停... _netStream.pause(); } //Stop按钮点击事件 function btnStopMouseClick(e:MouseEvent) { _netStream.pause(); _netStream.seek(0); //切换播放/暂停按钮状态 btnPause.visible=false; btnPlay.visible=true; } //获取视频总持续时间 function MetaDataHandler(data:Object) { _duration = data.duration; } function VolumnChangedHandler(e:ValueChangeEvent):void{ //如果没被禁音,则允许调节音量 if (btnSound.currentFrame==1){ SetVolumn(e.NewValue); } } function VideoValueChangedHandler(e:ValueChangeEvent):void{ _netStream.pause(); _netStream.seek(e.NewValue * _duration); //_netStream.resume(); } //进入每帧的处理函数 function EnterFrameHandler(e:Event):void{ if (_duration>0){ txtCurrent.text = Math.floor(_netStream.time/60) + ":" + Math.round(_netStream.time%60); txtTotal.text = Math.floor(_duration/60) + ":" + Math.round(_duration%60); silderVideo.Value = _netStream.time/_duration; } if (_netStream.bytesLoaded>0){ silderVideo.LoadingValue = _netStream.bytesLoaded / _netStream.bytesTotal; } //trace(_netStream.bytesLoaded + "/" + _netStream.bytesTotal); } function RemoveEnterFrameHandler(){ if (silderVideo.IsDragging){ removeEventListener(Event.ENTER_FRAME,EnterFrameHandler); } else{ addEventListener(Event.ENTER_FRAME,EnterFrameHandler); } } function silderVideoMouseDown(e:MouseEvent):void{ //trace("进度滑块被点击了!"); removeEventListener(Event.ENTER_FRAME,EnterFrameHandler); stage.addEventListener(MouseEvent.MOUSE_UP,stageMouseUpHandler); } function stageMouseUpHandler(e:MouseEvent):void{ addEventListener(Event.ENTER_FRAME,EnterFrameHandler); //trace("鼠标MouseUp事件"); if (btnPause.visible){ _netStream.resume(); } stage.removeEventListener(MouseEvent.MOUSE_UP,stageMouseUpHandler); } init();
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。