·您当前的位置:主页 > 技术教程 > 播放器教程 >

[AS3]AS3一个视频播放器源代码

时间:2016-02-25 10:01酷播
[AS3]AS3一个视频播放器源代码

[AS3]AS3一个视频播放器源代码

  1. package 
  2.     import com.bit101.components.HSlider; 
  3.     import com.bit101.components.InputText; 
  4.     import com.bit101.components.Label; 
  5.     import com.bit101.components.PushButton; 
  6.     import flash.display.Sprite; 
  7.     import flash.events.Event; 
  8.     import flash.events.MouseEvent; 
  9.     import flash.events.NetStatusEvent; 
  10.     import flash.media.SoundTransform; 
  11.     import flash.media.Video; 
  12.     import flash.net.NetConnection; 
  13.     import flash.net.NetStream; 
  14.   
  15.     [swf(width="500"height="500")] 
  16.     public class videoDisplay extends Sprite 
  17.     { 
  18.         private var _netStream:NetStream; 
  19.         private var _sp:Sprite; 
  20.         private var _bool:Boolean;//判断滑块是否按下,在每帧事件上侦听 
  21.         private var _hRangeSlider:HSlider;//视频加载位置 
  22.         private var _hRangeSlider2:HSlider;//视频播放位置 
  23.         private var _lable:Label;//显示当前播放时间/总时间 
  24.         private var _play:PushButton;//播放和暂停按钮 
  25.         private var _source:HSlider;//调整时声音滑块 
  26.         private var _sourceNum:Number = 0.6;//初始声音 
  27.         private var _soundOff:PushButton;//静音和开放按钮 
  28.         private var _isPlay:Boolean = false;//是否在播放 
  29.         private var _duration:Number;//返回总时间,秒为单位 
  30.           
  31.         //静音,0为静音 
  32.         private var sounceNum:int =1
  33.         private var _soundTransform:SoundTransform; 
  34.           
  35.         public function videoDisplay() 
  36.         { 
  37.             _bool = false;  
  38.               
  39.             //绘制控件 
  40.             Draw(); 
  41.               
  42.             //视频连接和播放主文件start 
  43.             var _netConnection:NetConnection = new NetConnection(); 
  44.             _netConnection.connect(null); 
  45.             var _clientOb:Object = new Object(); 
  46.             _clientOb.onMetaData = onMD
  47.             _netStream = new NetStream(_netConnection); 
  48.             _netStream.bufferTime = 15
  49.             _netStream.client = _clientOb
  50.             var _video:Video = new Video(); 
  51.             _video.attachNetStream(_netStream); 
  52.             _video.y = 100
  53.             addChild(_video); 
  54.             _netStream.play("videos/a.flv"); 
  55.             //end 
  56.               
  57.             this.addEventListener(Event.ENTER_FRAME, onEnterFrame); 
  58.             //视频状态报告 
  59.             _netStream.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus); 
  60.         } 
  61.   
  62.         private function Draw():void 
  63.         { 
  64.             //插件使用MinimalComps第三方插件 
  65.             _sp = new Sprite(); 
  66.             this.addChild(_sp); 
  67.             _hRangeSlider = new HSlider(_sp); 
  68.             _lable = new Label(_sp,_hRangeSlider.x+_hRangeSlider.width+10); 
  69.             _hRangeSlider2 = new HSlider(_sp, 0, _hRangeSlider.height+2); 
  70.             _hRangeSlider2.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); 
  71.             _lable.text = "00:00/00:00"
  72.             _playnew PushButton(_sp, 0, _hRangeSlider2.y + _hRangeSlider2.height+2,"pause",onPlay); 
  73.             _source = new HSlider(_sp,0,_play.y +_play.height+2) 
  74.             _source.value = (0.6/1)*_source.width; 
  75.             _source.addEventListener(MouseEvent.MOUSE_DOWN, onSoundMouseDown); 
  76.             _soundOff = new PushButton(_sp,0, _source.y+_source.height+2,"SounceOn",handSoundOff);  
  77.         } 
  78.           
  79.         private function onNetStatus(evt:NetStatusEvent):void 
  80.         { 
  81.             switch(evt.info.code) 
  82.             { 
  83.             case "NetStream.Play.Start"://开始播放 
  84.                 _isPlay = true
  85.                 break; 
  86.             case "NetStream.Play.Stop"://全部播放完 
  87.                 _isPlay =false
  88.                 break; 
  89.             case "NetStream.Buffer.Empty"://缓冲 
  90.                 break; 
  91.             } 
  92.         } 
  93.           
  94.         //点击播放暂停按钮 
  95.         private function onPlay(evt:Event):void 
  96.         { 
  97.               
  98.             if(_play.label  == "play") 
  99.             { 
  100.                 if(_isPlay)//是否在播放中,(包括暂停) 
  101.                 { 
  102.                     //_netStream.resume(); 
  103.                     _netStream.togglePause(); 
  104.                 } 
  105.                 else 
  106.                 { 
  107.                     _netStream.play("videos/a.flv"); 
  108.                     //_netStream.pause(); 
  109.                 } 
  110.             } 
  111.             if(_play.label  == "pause") 
  112.             { 
  113.                 _netStream.togglePause(); 
  114.             } 
  115.             _play_play.label = _play.label == "play"?"pause":"play"; 
  116.         } 
  117.           
  118.         //点击视频播放位置滑块 
  119.         private function onMouseDown(evt:Event):void 
  120.         { 
  121.             _bool = true
  122.             _netStream.togglePause();//暂停 
  123.             _hRangeSlider.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); 
  124.         } 
  125.         //点击视频播放位置滑块 
  126.         private function onMouseUp(evt:Event):void 
  127.         { 
  128.             _bool = false
  129.             _netStream.togglePause();//播放 
  130.             _hRangeSlider.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);        
  131.         } 
  132.           
  133.           
  134.           
  135.         private function onMD(data:Object):void 
  136.         { 
  137.             _duration = data.duration; 
  138.         } 
  139.         private function onEnterFrame(evt:Event):void 
  140.         { 
  141.             _lable.text = Math.floor(_netStream.time/60) +":"+ Math.round(_netStream.time%60)+"/" 
  142.                 +Math.floor(_duration/60) +":"+ Math.round(_duration%60); 
  143.               
  144.             var bigLoad:int = _netStream.bytesTotal;//视频文件大小 
  145.             var theLoad:int = _netStream.bytesLoaded;//当前加载大小 
  146.             _hRangeSlider.value = (theLoad/bigLoad)*_hRangeSlider.width; 
  147.             if(_bool) 
  148.             { 
  149.                 //播放视频的指定位置 
  150.                 _netStream.seek((_hRangeSlider2.value/_hRangeSlider2.width)*_duration); 
  151.             } 
  152.             else 
  153.             { 
  154.                 _hRangeSlider2.value = (_netStream.time/_duration)*_hRangeSlider2.width; 
  155.             } 
  156.         } 
  157.   
  158.         //根据传入0-1之间的数调节音量 
  159.         private function setSource(num:Number):void 
  160.         { 
  161.             _soundTransform = _netStream.soundTransform; 
  162.             _soundTransform.volume = num
  163.             _netStream.soundTransform = _soundTransform
  164.         } 
  165.           
  166.         //静音和开发 
  167.         private function handSoundOff(EVT:Event):void 
  168.         { 
  169.             _soundOff_soundOff.label =_soundOff.label == "SounceOff"?"SounceOn":"SounceOff"; 
  170.             if(_soundOff.label == "SounceOff") 
  171.             { 
  172.                 sounceNum = 0
  173.                 setSource(0); 
  174.             } 
  175.             else 
  176.             { 
  177.                 sounceNum = 1
  178.                 setSource(Math.round((_source.value/_source.width)*10)/10); 
  179.             } 
  180.         } 
  181.           
  182.         //拖动音量滑块到指定位置 
  183.         private function onSoundMouseDown(evt:Event):void 
  184.         { 
  185.             _source.stage.addEventListener(MouseEvent.MOUSE_UP, onSoundMouseUp); 
  186.         } 
  187.         private function onSoundMouseUp(evt:Event):void 
  188.         { 
  189.             _source.stage.removeEventListener(MouseEvent.MOUSE_UP, onSoundMouseUp); 
  190.             if(sounceNum)//非静音 
  191.             { 
  192.                 setSource(Math.round((_source.value/_source.width)*10)/10);     
  193.             } 
  194.         } 
  195.     } 

 

热门文章推荐

请稍候...