·您当前的位置:首页 > 技术教程 > Flex技术 >

[flex]flex视频监控分区多屏播放器源代码(2)

时间:2014-02-19 10:10blog.csdn.net
组件(FLVideo.mxml) ? xml version = 1.0 encoding = utf-8 ? s:Group xmlns:fx = http://ns.adobe.com/mxml/2009 xmlns:s = library://ns.adobe.com/flex/spark xmlns:mx = library://ns.adobe.com/flex/mx s:la

组件(FLVideo.mxml)

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"  
  3.          xmlns:s="library://ns.adobe.com/flex/spark"  
  4.          xmlns:mx="library://ns.adobe.com/flex/mx"> 
  5.     <s:layout> 
  6.         <s:BasicLayout/> 
  7.     </s:layout> 
  8.     <fx:Script> 
  9.         <![CDATA[ 
  10.             import mx.controls.Alert; 
  11.             import mx.events.FlexEvent; 
  12.             import mx.events.ResizeEvent; 
  13.             private var conn:NetConnection;  
  14.             private var stream:NetStream;  
  15.             private var video:Video; 
  16.             [Bindable] 
  17.             private var _videoHost:String = ""; //视频服务器地址 
  18.             [Bindable] 
  19.             private var _videoName:String = ""; //视频名称 
  20.             [Bindable] 
  21.             private var visibleFlag:Boolean = false; 
  22.              
  23.             public var isConStream:Boolean = false; 
  24.              
  25.             public function set videoHost(host:String):void{ 
  26.                 this._videoHost = host; 
  27.             } 
  28.             public function get videoHost():String{ 
  29.                 return this._videoHost; 
  30.             } 
  31.              
  32.             public function set videoName(name:String):void{ 
  33.                 this._videoName = name; 
  34.             } 
  35.             public function get videoName():String{ 
  36.                 return this._videoName; 
  37.             } 
  38.             //用于主应用程序调用初始化连接 
  39.             public function init(videoHost:String, videoName:String):void 
  40.             { 
  41.                 // TODO Auto-generated method stub 
  42.                 _videoName = videoName; 
  43.                 _videoHost = videoHost; 
  44.                 conn=new NetConnection;  
  45.                 conn.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);  
  46.                 conn.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);  
  47.                 conn.client = this; 
  48.                 conn.connect(videoHost);    //连接Red5服务器 
  49.             } 
  50.              
  51.             public function close():void{ 
  52.                 if(conn != null){ 
  53.                     conn.close(); 
  54.                 } 
  55.                 if(stream != null){ 
  56.                     stream.close(); 
  57.                 } 
  58.                 if(video != null){ 
  59.                     video.clear(); 
  60.                 } 
  61.                 isConStream = false; 
  62.             } 
  63.              
  64.             private function securityErrorHandler(event:SecurityErrorEvent):void{  
  65.             }  
  66.             private function asyncErrorHandler(event:AsyncErrorEvent):void{  
  67.             }  
  68.             public function onBWCheck(... arg):void 
  69.             { 
  70.                 //do nothing 
  71.             } 
  72.             public function onBWDone(... arg):void {  
  73.                  
  74.             } 
  75.             public function onMetaData(obj:Object):void{ 
  76.                 trace(obj.duration); 
  77.             } 
  78.             private function netStatusHandler(event:NetStatusEvent):void{  
  79.                 trace(event.info.code); 
  80.                 switch(event.info.code)  
  81.                 {  
  82.                     case "NetConnection.Connect.Success":  
  83.                     {  
  84.                         visibleFlag = true; //连接成功时缓冲设为可见 
  85.                         connectStream();  
  86.                         break;  
  87.                     }  
  88.                     case "NetStream.Play.StreamNotFound":{  
  89.                         Alert.show("文件不存在!","提示");  
  90.                         break;  
  91.                     }    
  92.                          
  93.                     case "NetStream.Buffer.Full":{ 
  94.                         visibleFlag = false; //缓冲到可以播放隐藏 
  95.                         break; 
  96.                     } 
  97.                     case "NetStream.Buffer.Empty":{ 
  98.                         visibleFlag = true; //获取不到缓冲流的时候显示 
  99.                         break; 
  100.                     } 
  101.                     case "NetConnection.Connect.Closed":{ 
  102.                         visibleFlag = false; //关闭连接的时候显示 
  103.                         break; 
  104.                     } 
  105.                 }  
  106.             }  
  107.             private function connectStream():void{  
  108.                 stream=new NetStream(conn);  
  109.                 stream.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);  
  110.                 stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncErrorHandler);  
  111.                 stream.client = {}; //这句一定要,不然报错找不到onMetaData方法 
  112.                 stream.bufferTime = 1; 
  113.                 video = new Video();  //定义一个播放器  
  114.                 video.width = vd.width;  
  115.                 video.height = vd.height;  
  116.                 video.smoothing = true; 
  117.                 video.attachNetStream(stream); 
  118.                 stream.play(_videoName);  
  119.                 isConStream = true; //连接播放成功设为true   
  120.                 vd.addChild(video); 
  121.             }  
  122.              
  123.             protected function vd_resizeHandler(event:ResizeEvent):void 
  124.             { 
  125.                 // TODO Auto-generated method stub 
  126.                 if(video != null){ 
  127.                     video.width = vd.width; 
  128.                     video.height = vd.height; 
  129. //                  swfLoader.width =  
  130.                 } 
  131.             } 
  132.              
  133.         ]]> 
  134.     </fx:Script> 
  135.     <fx:Declarations> 
  136.         <!-- 将非可视元素(例如服务、值对象)放在此处 --> 
  137.     </fx:Declarations> 
  138.      
  139.     <mx:VideoDisplay id="vd" width="100%" height="100%" contentBackgroundColor="0x666666" resize="vd_resizeHandler(event)"/> 
  140.     <s:SWFLoader horizontalCenter="2" verticalCenter="2" 
  141.                  id="swfLoader" source="com/bx/assets/loading1.swf" width="65" height="55" visible="{visibleFlag}"/>  
  142. </s:Group> 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器