·您当前的位置:主页 > 技术教程 > RED5教程 >

[red5直播]red5做视频会议直播

时间:2016-06-12 15:52酷播
公司最近要在系统中加视频会议的功能,让我探索,我选择了最流行的red5来实现

公司最近要在系统中加视频会议的功能,让我探索,我选择了最流行的red5来实现,网上有一对一聊天的demo,找不到多对多聊天的,也没有具体介 绍系统搭建的过程,我通过自己的摸索,将实现的过程和大家一起分享。java的web项目添加flex支持在此不再详述,项目文件结构如图:


  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">   
  3.     <mx:Script>   
  4.         <![CDATA[   
  5.             import mx.collections.ArrayCollection;   
  6.             import mx.containers.HBox;   
  7.             import mx.controls.Alert;   
  8.                
  9.                
  10.             private var listSO:SharedObject;   
  11.             private var userArr:Array;   
  12.             private var conn:NetConnection;   
  13.             private var localUsername:String;   
  14.             [Bindable]   
  15.             private var cam:Camera;   
  16.             [Bindable]   
  17.             private var mic:Microphone;   
  18.             [Bindable]   
  19.             public var cards:ArrayCollection;   
  20.             public var videoUsers:Array;   
  21.             [Bindable]   
  22.             public var videoControlArr:Array;   
  23.                
  24.             private var stm:NetStream;   
  25.             [Bindable]   
  26.             private var video_self:Video;   
  27.                
  28.             protected function login(event:MouseEvent):void   
  29.             {   
  30.                 localUsername = txt_name.text;   
  31.                 if(localUsername== ""){   
  32.                     Alert.show("用户名不能为空");   
  33.                 }else{   
  34.                     if(conn == null){   
  35.                         conn = new NetConnection();   
  36.                         conn.client = this;   
  37.                         conn.addEventListener(NetStatusEvent.NET_STATUS,_statusHandler);   
  38.                         conn.connect("rtmp://192.168.1.61/Red5ChatRoom",localUsername);   
  39.                     }   
  40.                 }   
  41.             }   
  42.                
  43.             //状态监听   
  44.             private function _statusHandler(evt:NetStatusEvent):void   
  45.             {   
  46.                 if(evt.info.code == "NetConnection.Connect.Success"){   
  47.                     this.currentState = "chat";   
  48.                     Alert.show("连接成功");   
  49.                     video_clickHandler();   
  50.                     this.showJoinInInfo(localUsername);   
  51.                     _setListSO();   
  52.                        
  53.                        
  54.                 }   
  55.                 if(evt.info.code == "NetConnection.Connect.Failed"){   
  56.                     Alert.show("连接失败");   
  57.                 }   
  58.                 if(evt.info.code == "NetConnection.Connect.Closed"){   
  59.                     Alert.show("连接关闭");   
  60.                 }   
  61.             }   
  62.                
  63.             public function showJoinInInfo(message:String):void   
  64.             {   
  65.                 conn.call("getOnloadUser",null,message);   
  66.             }   
  67.                
  68.             public function result_getOnloadUser(str:String):void{   
  69.                 txt_chatmsg.text += str + "加入聊天室" + "\n";   
  70.             }   
  71.             //创建用户列表共享对象   
  72.             private function _setListSO():void   
  73.             {   
  74.                 listSO = SharedObject.getRemote("listSO",conn.uri,false);   
  75.                 listSO.connect(conn);   
  76.                 listSO.addEventListener(SyncEvent.SYNC,_listSOSyncHandler);   
  77.             }   
  78.                
  79.             //用户列表共享对象被更新之后的事件   
  80.             private function _listSOSyncHandler(evt:SyncEvent):void{   
  81.                 _showUserList();//更新用户列表   
  82.             }   
  83.                
  84.             private function _showUserList():void   
  85.             {   
  86.                 cards = new ArrayCollection(   
  87.                     [{label:"All"}]   
  88.                 );   
  89.                 userArr = new Array();   
  90.                 //用户数组更新   
  91.                 for(var tmp:String in listSO.data){   
  92.                     userArr.push(listSO.data[tmp]);   
  93.                 }   
  94.                 //添加到arrayCollection   
  95.                 for(var i:int = 0; i<userArr.length;i++){   
  96.                     cards.addItem({label:userArr[i]});   
  97.                 }   
  98.                 //将数组添加到列表数组中显示出来   
  99.                 userList.dataProvider = cards;   
  100.                 users.dataProvider = cards;   
  101.                 addVideo(cards);   
  102.             }   
  103.                
  104.             public function showMessage(message:String):void   
  105.             {   
  106.                 txt_chatmsg.text += message + "\n";   
  107.             }   
  108.                
  109.             protected function sendMessage(event:MouseEvent):void   
  110.             {   
  111.                 var sendString:String = txt_yousay.text;   
  112.                 var sendTo:String = userList.selectedItem.label;   
  113.                 txt_yousay.text = "";   
  114.                 conn.call("sayToAll",null,sendTo,sendString);   
  115.             }   
  116.                
  117.             //断线通知   
  118.             public function disconnectMessage(disUser:String):void   
  119.             {   
  120.                 txt_chatmsg.text += disUser+"退出聊天室\n";   
  121.             }   
  122.             //进入视频会议   
  123.             public function video_clickHandler():void   
  124.             {   
  125.                 stm = new NetStream(conn);   
  126.                 cam = Camera.getCamera();   
  127.                 if(cam==null){   
  128.                     Alert.show("没有可以使用的摄像头");   
  129.                     return;   
  130.                 }else{   
  131.                     Security.showSettings(SecurityPanel.PRIVACY);   
  132.                     cam.addEventListener(StatusEvent.STATUS,statusHandler);   
  133.                     cam.addEventListener(ActivityEvent.ACTIVITY,activityHandler);   
  134.                     cam.setLoopback(true);   
  135.                     cam.setMotionLevel(50,100);   
  136.                     cam.setMode(1280,960,15,true);    
  137.                     stm.attachCamera(cam);   
  138.                 }   
  139.                 mic = Microphone.getMicrophone();   
  140.                 mic.addEventListener(StatusEvent.STATUS,micOnstatu);   
  141.                 if(mic == null){   
  142.                     Alert.show("没有可以使用的麦克风");   
  143.                 }else{   
  144.                     mic.setUseEchoSuppression(true);   
  145.                     stm.attachAudio(mic);   
  146.                 }   
  147.                 stm.play("chinahrt-"+txt_name.text);   
  148.                 stm.publish("chinahrt-"+txt_name.text,"live");   
  149.                 video_self = new Video();   
  150.                 video_self.width = 320;   
  151.                 video_self.height = 240;   
  152.                 video_self.attachCamera(cam);   
  153.                 my_video.addChild(video_self);   
  154.                    
  155.             }   
  156.                
  157.             private function micOnstatu(e:StatusEvent):void   
  158.             {   
  159.                 mic.setLoopBack(true);   
  160.                 mic.gain = 66;   
  161.                 mic.rate = 11;   
  162.                 mic.setUseEchoSuppression(true);   
  163.                 mic.setSilenceLevel(1,-1);   
  164.             }   
  165.                
  166.             private function statusHandler(e:StatusEvent):void   
  167.             {   
  168.                    
  169.             }   
  170.                
  171.             private function activityHandler(e:ActivityEvent):void   
  172.             {   
  173.                
  174.             }   
  175.                
  176.             private function addVideo(cards:ArrayCollection):void   
  177.             {   
  178.                 label1.text = "我的("+localUsername+")";   
  179.                 myBox.removeAllChildren();   
  180.                 var otherPerson:ArrayCollection = new ArrayCollection();   
  181.                 for(var i:int=0;i<cards.length;i++){   
  182.                     var o:Object = cards.getItemAt(i);   
  183.                     if(o["label"]!=localUsername&&o["label"]!="All"){   
  184.                         otherPerson.addItem(o);            
  185.                     }   
  186.                 }   
  187.         //      Alert.show(otherPerson.length+"");   
  188.                 var yushu:int = 0;   
  189.                 var yushu:int= otherPerson.length%3;   
  190.                 var rowNum:int = 0;   
  191.                 var rowNum:int = otherPerson.length/3;   
  192.                    
  193.                 if(yushu!=0){   
  194.                     rowNum += 1;   
  195.                 }   
  196.                 if(yushu==0){   
  197.                     for(var i:int=0;i<rowNum;i++){   
  198.                         var hbox:HBox = new HBox();   
  199.                         myBox.addChild(hbox);   
  200.                         for(var ii:int=0;ii<3;ii++){   
  201.                             var vbox:VBox = new VBox();   
  202.                             hbox.addChild(vbox);   
  203.                             var label:Label = new Label;   
  204.                             label.text = otherPerson.getItemAt(i*3+ii)["label"];   
  205.                             vbox.addChild(label);   
  206.                             var videoDisplay:VideoDisplay = new VideoDisplay();   
  207.                             videoDisplay.live = true;   
  208.                             videoDisplay.width = 320;   
  209.                             videoDisplay.height = 240;   
  210.                             vbox.addChild(videoDisplay);   
  211.                             var video:Video = new Video();   
  212.                             video.width = 320;   
  213.                             video.height = 240;   
  214.                             var netStream:NetStream = new NetStream(conn);   
  215.                             video.attachNetStream(netStream);   
  216.                             netStream.play("chinahrt-"+label.text);   
  217.                                
  218.                             videoDisplay.addChild(video);   
  219.                         }   
  220.                     }   
  221.                 }else{   
  222.                     for(var i:int=0;i<rowNum-1;i++){   
  223.                         var hbox:HBox = new HBox();   
  224.                         myBox.addChild(hbox);   
  225.                         for(var ii:int=0;ii<3;ii++){   
  226.                             var vbox:VBox = new VBox();   
  227.                             hbox.addChild(vbox);   
  228.                             var label:Label = new Label;   
  229.                             label.text = otherPerson.getItemAt(i*3+ii)["label"];   
  230.                             vbox.addChild(label);   
  231.                             var videoDisplay:VideoDisplay = new VideoDisplay();   
  232.                             videoDisplay.live = true;   
  233.                             videoDisplay.width = 320;   
  234.                             videoDisplay.height = 240;   
  235.                             vbox.addChild(videoDisplay);   
  236.                             var video:Video = new Video();   
  237.                             video.width = 320;   
  238.                             video.height = 240;   
  239.                             var netStream:NetStream = new NetStream(conn);   
  240.                             video.attachNetStream(netStream);   
  241.                             netStream.play("chinahrt-"+label.text);   
  242.                             videoDisplay.addChild(video);   
  243.                         }   
  244.                     }   
  245.                     var hbox:HBox = new HBox();   
  246.                     myBox.addChild(hbox);   
  247.                     for(var i:int=0;i<yushu;i++){   
  248.                         var vbox:VBox = new VBox();   
  249.                         hbox.addChild(vbox);   
  250.                     //  myBox.addChild(vbox);   
  251.                         var label:Label = new Label();   
  252.                         label.text = otherPerson.getItemAt((rowNum-1)*3+i)["label"];   
  253.                         vbox.addChild(label);   
  254.                         var videoDisplay:VideoDisplay = new VideoDisplay();   
  255.                         videoDisplay.live = true;   
  256.                         videoDisplay.width = 320;   
  257.                         videoDisplay.height = 240;   
  258.                         vbox.addChild(videoDisplay);   
  259.                         var video:Video = new Video();   
  260.                         video.width = 320;   
  261.                         video.height = 240;   
  262.                         var netStream:NetStream = new NetStream(conn);   
  263.                     //  Alert.show(otherPerson.getItemAt((rowNum-1)*3+i)["label"]);   
  264.                         video.attachNetStream(netStream);   
  265.                         netStream.play("chinahrt-"+label.text);   
  266.                            
  267.                         videoDisplay.addChild(video);   
  268.                     }   
  269.                 }   
  270.                    
  271.             }   
  272.         ]]>   
  273.     </mx:Script>   
  274.        
  275.     <mx:states>    
  276.         <mx:State id="chatState" name="chat">    
  277.             <mx:SetProperty target="{form1}" name="width" value="0"/>    
  278.             <mx:SetProperty target="{form1}" name="height" value="0"/>    
  279.             <mx:SetProperty target="{form1}" name="x" value="0"/>    
  280.             <mx:SetProperty target="{form1}" name="y" value="0"/>    
  281.             <mx:AddChild position="lastChild">    
  282.                 <mx:Panel x="10" y="10" width="381" height="370" layout="absolute" title="聊天信息">    
  283.                        
  284.                     <mx:TextArea x="10" y="10" width="215" height="235" id="txt_chatmsg"/>    
  285.                     <mx:ComboBox x="233" y="34" width="118" id="userList"></mx:ComboBox>    
  286.                     <mx:Label x="233" y="11" text="用户列表"/>    
  287.                     <mx:DataGrid x="233" y="64" height="256" id="users" width="118">    
  288.                         <mx:columns>    
  289.                             <mx:DataGridColumn headerText="用户名" dataField="label"/>    
  290.                         </mx:columns>    
  291.                     </mx:DataGrid>    
  292.                     <mx:TextInput x="10" y="253" height="67" width="150" id="txt_yousay"/>    
  293.                     <mx:Button x="168" y="253" label="发送" width="57" click="sendMessage(event)"/>    
  294.                 <!-- <mx:Button x="168" y="298" label="进入视频会议" width="57" click="video_clickHandler(event)"/>-->    
  295.                 </mx:Panel>    
  296.             </mx:AddChild>    
  297.             <mx:AddChild position="lastChild">    
  298.                 <mx:VideoDisplay live="true" x="10" y="410"  width="320" height="240" id="my_video"/>    
  299.             </mx:AddChild>    
  300.             <mx:AddChild position="lastChild">    
  301.                 <mx:Label id="label1" x="10" y="390" text="我的"/>    
  302.             </mx:AddChild>    
  303.             <!--<mx:AddChild position="lastChild">    
  304.                 <mx:VideoDisplay live="true"  x="399" y="224"  width="320" height="240" id="other_video"/>    
  305.             </mx:AddChild>    
  306.             <mx:AddChild position="lastChild">    
  307.                 <mx:Label x="399" y="198" text="对方的"/>    
  308.             </mx:AddChild> -->   
  309.             <mx:AddChild position="lastChild">   
  310.                 <mx:VBox id="myBox" x="399" y="10"/>   
  311.             </mx:AddChild>   
  312.         </mx:State>    
  313.     </mx:states>    
  314.        
  315.     <mx:Form x="10" y="10" width="283" height="126" id="form1">    
  316.         <mx:FormItem label="用户名:">    
  317.             <mx:TextInput id="txt_name"/>    
  318.         </mx:FormItem>    
  319.         <mx:FormItem>    
  320.             <mx:Button label="登陆" click="login(event)"/>    
  321.         </mx:FormItem>    
  322.     </mx:Form>    
  323. </mx:Application>   

 

热门文章推荐

请稍候...