package { import com.bit101.components.HSlider; import com.bit101.components.InputText; import com.bit101.components.Label; import com.bit101.components.PushButton; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.events.NetStatusEvent; import flash.media.SoundTransform; import flash.media.Video; import flash.net.NetConnection; import flash.net.NetStream; [swf(width="500", height="500")] public class videoDisplay extends Sprite { private var _netStream:NetStream; private var _sp:Sprite; private var _bool:Boolean;//判断滑块是否按下,在每帧事件上侦听 private var _hRangeSlider:HSlider;//视频加载位置 private var _hRangeSlider2:HSlider;//视频播放位置 private var _lable:Label;//显示当前播放时间/总时间 private var _play:PushButton;//播放和暂停按钮 private var _source:HSlider;//调整时声音滑块 private var _sourceNum:Number = 0.6;//初始声音 private var _soundOff:PushButton;//静音和开放按钮 private var _isPlay:Boolean = false;//是否在播放 private var _duration:Number;//返回总时间,秒为单位 //静音,0为静音 private var sounceNum:int =1; private var _soundTransform:SoundTransform; public function videoDisplay() { _bool = false; //绘制控件 Draw(); //视频连接和播放主文件start var _netConnection:NetConnection = new NetConnection(); _netConnection.connect(null); var _clientOb:Object = new Object(); _clientOb.onMetaData = onMD; _netStream = new NetStream(_netConnection); _netStream.bufferTime = 15; _netStream.client = _clientOb; var _video:Video = new Video(); _video.attachNetStream(_netStream); _video.y = 100; addChild(_video); _netStream.play("videos/a.flv"); //end this.addEventListener(Event.ENTER_FRAME, onEnterFrame); //视频状态报告 _netStream.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus); } private function Draw():void { //插件使用MinimalComps第三方插件 _sp = new Sprite(); this.addChild(_sp); _hRangeSlider = new HSlider(_sp); _lable = new Label(_sp,_hRangeSlider.x+_hRangeSlider.width+10); _hRangeSlider2 = new HSlider(_sp, 0, _hRangeSlider.height+2); _hRangeSlider2.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); _lable.text = "00:00/00:00"; _play= new PushButton(_sp, 0, _hRangeSlider2.y + _hRangeSlider2.height+2,"pause",onPlay); _source = new HSlider(_sp,0,_play.y +_play.height+2) _source.value = (0.6/1)*_source.width; _source.addEventListener(MouseEvent.MOUSE_DOWN, onSoundMouseDown); _soundOff = new PushButton(_sp,0, _source.y+_source.height+2,"SounceOn",handSoundOff); } private function onNetStatus(evt:NetStatusEvent):void { switch(evt.info.code) { case "NetStream.Play.Start"://开始播放 _isPlay = true; break; case "NetStream.Play.Stop"://全部播放完 _isPlay =false; break; case "NetStream.Buffer.Empty"://缓冲 break; } } //点击播放暂停按钮 private function onPlay(evt:Event):void { if(_play.label == "play") { if(_isPlay)//是否在播放中,(包括暂停) { //_netStream.resume(); _netStream.togglePause(); } else { _netStream.play("videos/a.flv"); //_netStream.pause(); } } if(_play.label == "pause") { _netStream.togglePause(); } _play.label = _play.label == "play"?"pause":"play"; } //点击视频播放位置滑块 private function onMouseDown(evt:Event):void { _bool = true; _netStream.togglePause();//暂停 _hRangeSlider.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); } //点击视频播放位置滑块 private function onMouseUp(evt:Event):void { _bool = false; _netStream.togglePause();//播放 _hRangeSlider.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp); } private function onMD(data:Object):void { _duration = data.duration; } private function onEnterFrame(evt:Event):void { _lable.text = Math.floor(_netStream.time/60) +":"+ Math.round(_netStream.time%60)+"/" +Math.floor(_duration/60) +":"+ Math.round(_duration%60); var bigLoad:int = _netStream.bytesTotal;//视频文件大小 var theLoad:int = _netStream.bytesLoaded;//当前加载大小 _hRangeSlider.value = (theLoad/bigLoad)*_hRangeSlider.width; if(_bool) { //播放视频的指定位置 _netStream.seek((_hRangeSlider2.value/_hRangeSlider2.width)*_duration); } else { _hRangeSlider2.value = (_netStream.time/_duration)*_hRangeSlider2.width; } } //根据传入0-1之间的数调节音量 private function setSource(num:Number):void { _soundTransform = _netStream.soundTransform; _soundTransform.volume = num; _netStream.soundTransform = _soundTransform; } //静音和开发 private function handSoundOff(EVT:Event):void { _soundOff.label =_soundOff.label == "SounceOff"?"SounceOn":"SounceOff"; if(_soundOff.label == "SounceOff") { sounceNum = 0; setSource(0); } else { sounceNum = 1; setSource(Math.round((_source.value/_source.width)*10)/10); } } //拖动音量滑块到指定位置 private function onSoundMouseDown(evt:Event):void { _source.stage.addEventListener(MouseEvent.MOUSE_UP, onSoundMouseUp); } private function onSoundMouseUp(evt:Event):void { _source.stage.removeEventListener(MouseEvent.MOUSE_UP, onSoundMouseUp); if(sounceNum)//非静音 { setSource(Math.round((_source.value/_source.width)*10)/10); } } } }