·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

flex4+fms3.5+cs4开发实时音视频直播及点播详解[组图](2)

时间:2012-07-12 16:27csdn.net
flex部分 ? xml version = 1.0 encoding = utf-8 ? s:WindowedApplication xmlns:fx = http://ns.adobe.com/mxml/2009 xmlns:s = library://ns.adobe.com/flex/spark xmlns:mx = library://ns.adobe.com/flex/mx wi

flex部分

  1.  
  2. <?xml version="1.0" encoding="utf-8"?> 
  3. <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"   
  4.                        xmlns:s="library://ns.adobe.com/flex/spark"   
  5.                        xmlns:mx="library://ns.adobe.com/flex/mx" width="430" height="340"   
  6.                        creationComplete="init_creationCompleteHandler(event)"   
  7.                        closing="unonload()"> 
  8.     <s:layout> 
  9.         <s:BasicLayout/> 
  10.     </s:layout> 
  11.     <fx:Style source="media.css"/> 
  12.  
  13.     <fx:Script> 
  14.         <!--[CDATA[  
  15.             import flash.events.NetStatusEvent;  
  16.             import flash.net.NetConnection;  
  17.             import flash.net.NetStream;  
  18.               
  19.             import mx.collections.ArrayCollection;  
  20.             import mx.controls.Alert;  
  21.             import mx.controls.Text;  
  22.             import mx.events.FlexEvent;  
  23.               
  24.             private var nc:NetConnection;     
  25.             private var ns:NetStream;     
  26.             private var vi:Video;  
  27.             private var cam:Camera;     
  28.             private var microphone:Microphone;  
  29.             private var fmsArr:ArrayCollection=new ArrayCollection([]);  
  30.             private var sharedObject:SharedObject=SharedObject.getLocal("com.ue");   
  31.               
  32.             /**  
  33.              * 初始化  
  34.              */  
  35.             protected function init_creationCompleteHandler(event:FlexEvent):void{  
  36.                 var loader:URLLoader = new URLLoader();  
  37.                 loader.dataFormat=URLLoaderDataFormat.VARIABLES;  
  38.                 loader.addEventListener(Event.COMPLETE, loader_complete);  
  39.                 loader.load(new URLRequest("global.txt"));  
  40.                   
  41.                 function loader_complete(e:Event):void {  
  42.                     var variables:URLVariables=new URLVariables(loader.data);                 
  43.                     trace(variables["servers"]);  
  44.                                           
  45.                     var serversArr:Array = variables["servers"].toString().split("@#@");  
  46.                     for(var i:int=0;i<serversArr.length;i++){  
  47.                         var server:String = serversArr[i];  
  48.                         var sArr:Array = server.toString().split("$#$");  
  49.                           
  50.                         fmsArr.addItem({label:sArr[0], value:sArr[1]});  
  51.                     }  
  52.                 }  
  53.                   
  54.                 getCookie();  
  55.             }  
  56.               
  57.             public function getCookie():void{  
  58.                 if(sharedObject.data["pmhidh"]!=null){  
  59.                     pmhID.text = sharedObject.data["pmhidh"];  
  60.                 }  
  61.                 if(sharedObject.data["spfwq"]!=null){  
  62.                     trace(sharedObject.data["spfwq"]);  
  63.                     fms.selectedItem = sharedObject.data["spfwq"];  
  64.                 }  
  65.                 if(sharedObject.data["yl"]!=null){  
  66.                     slider.value = sharedObject.data["yl"];  
  67.                 }  
  68.                   
  69.             }  
  70.               
  71.             /**  
  72.              * 开始直播  
  73.              */  
  74.             protected function startButton_clickHandler(event:MouseEvent):void{  
  75.                   
  76.                 startButton.enabled=false;  
  77.                 stopButton.enabled=true;  
  78.                 nc = new NetConnection();     
  79.                 nc.addEventListener(NetStatusEvent.NET_STATUS,connectServerHander);     
  80.                 nc.client  = this;  
  81.                   
  82.                 trace(fms.selectedItem.label);  
  83.                 nc.connect(fms.selectedItem.value);    
  84.                   
  85.                 slider.enabled=true;  
  86.             }             
  87.             /**  
  88.              * 回调函数  
  89.              */  
  90.             public function connectServerHander(evt:NetStatusEvent):void{  
  91.                 cam = Camera.getCamera();     
  92.                 microphone = Microphone.getMicrophone();  
  93.                   
  94.                 if(microphone!=null){  
  95.                     microphone.setSilenceLevel(0);//总是收集音频  
  96.                     microphone.rate=44;  
  97.                 }else{  
  98.                     Alert.show("无法链接到麦克风!");   
  99.                 }  
  100.                   
  101.                 if(cam != null){     
  102.                     cam.setMode(176,144,25);     
  103.                     cam.setQuality(0,90);     
  104.                     ns = new NetStream(nc);      
  105.                     ns.attachCamera(cam);     
  106.                     ns.attachAudio(microphone);  
  107.                     ns.publish(pmhID.text,"append");     
  108.                       
  109.                     vi = new Video();     
  110.                     vi.width = player.width;     
  111.                     vi.height = player.height;     
  112.                     vi.attachCamera(cam);     
  113.                     player.addChild(vi);     
  114.                                           
  115.                 }else{     
  116.                     Alert.show("无法链接到摄像头!");     
  117.                 }     
  118.             }  
  119.             /**  
  120.              * NetConnection回调函数  
  121.              */  
  122.             public function onBWDone():void{}   
  123.               
  124.               
  125.             /**  
  126.              * 保存音视频文件  
  127.              */       
  128. //          protected function browse_clickHandler(event:MouseEvent):void{  
  129. //              var file:File = new File();   
  130. //              file.addEventListener(Event.SELECT,dirSelected);   
  131. //              file.browseForSave("另存为");   
  132. //              function dirSelected(e:Event):void {   
  133. //                  trace(file.nativePath);   
  134. //                  filefilePath.text = file.nativePath;  
  135. //              }   
  136. //          }  
  137.  
  138.             /**  
  139.              * 音量控制  
  140.              */  
  141.             protected function slider_changeHandler(event:Event):void{  
  142.                 microphone.gain = slider.value;  
  143.             }  
  144.  
  145.  
  146.             /**  
  147.              * 停止发布  
  148.              */  
  149.             protected function stopButton_clickHandler(event:MouseEvent):void{  
  150.                 ns.close();  
  151.                 vi.visible=false;  
  152.                 startButton.enabled=true;  
  153.                 stopButton.enabled=false;  
  154.             }  
  155.               
  156.             /**  
  157.              * 关闭窗口触发事件,保存配置  
  158.              */  
  159.             public function unonload():void{  
  160.                 if(sharedObject!=null){   
  161.                     sharedObject.clear();   
  162.                 }   
  163.                 if(sharedObject.data["pmhidh"]==null){  
  164.                     sharedObject.data["pmhidh"] = pmhID.text;  
  165.                 }  
  166.                 if(sharedObject.data["spfwq"]==null){  
  167.                     sharedObject.data["spfwq"] = fms.selectedItem;  
  168.                 }  
  169.                 if(sharedObject.data["yl"]==null){  
  170.                     sharedObject.data["yl"] = slider.value;  
  171.                 }     
  172.                 sharedObject.flush();  
  173.             }  
  174.  
  175.         ]]--> 
  176.     </fx:Script> 
  177.  
  178.     <fx:Declarations> 
  179.         <!-- 将非可视元素(例如服务、值对象)放在此处 --> 
  180.     </fx:Declarations> 
  181.     <s:Panel width="100%" height="100%" x="0" y="0" id="panel1"> 
  182.         <s:layout> 
  183.             <s:VerticalLayout paddingLeft="0" paddingRight="0" paddingBottom="0" paddingTop="0" requestedRowCount="7"/> 
  184.         </s:layout> 
  185.         <s:VGroup width="430" height="272" paddingTop="10" paddingBottom="0" paddingRight="0" paddingLeft="0"> 
  186.             <s:HGroup width="430" height="220" paddingLeft="5" paddingTop="2" paddingBottom="0" paddingRight="5"> 
  187.                 <s:BorderContainer width="206" height="215"> 
  188.                     <s:VideoDisplay width="193" height="202" scaleMode="letterbox" id="player" x="5" y="5"/> 
  189.                 </s:BorderContainer> 
  190.                 <s:BorderContainer width="206" height="215"> 
  191.                     <s:VGroup width="193" height="208" paddingLeft="5" paddingTop="0" paddingBottom="0"   
  192.  
  193. paddingRight="5" gap="9" x="5" y="5"> 
  194.                         <s:Label text="拍卖会ID号:" height="14" id="pmhIDLabel"/> 
  195.                         <s:TextInput id="pmhID"/> 
  196.                         <s:Label text="视频服务器:" height="14"/> 
  197.                         <s:ComboBox id="fms" dataProvider="{fmsArr}"/> 
  198.                         <s:Label text="音量" height="14"/> 
  199.                         <s:HSlider id="slider" enabled="false" change="slider_changeHandler(event)" minimum="0"   
  200.  
  201. maximum="100" stepSize="10"/> 
  202.                     </s:VGroup> 
  203.                 </s:BorderContainer> 
  204.             </s:HGroup> 
  205.             <s:HGroup width="430" height="35" paddingTop="0" paddingBottom="0" paddingRight="5" paddingLeft="145"   
  206.  
  207. verticalAlign="middle" textAlign="center"> 
  208.                 <s:Button id="startButton" label="开始发布" click="startButton_clickHandler(event)"/> 
  209.                 <s:Button label="停止发布" id="stopButton" enabled="false" click="stopButton_clickHandler(event)"/> 
  210.             </s:HGroup> 
  211.         </s:VGroup> 
  212.     </s:Panel> 
  213. </s:WindowedApplication> 

效果如下:

热门文章推荐

请稍候...

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

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