注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

老毛的日记本

 
 
 

日志

 
 

flex 半成品  

2011-11-23 16:58:51|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

视频会议半成品

 

 <?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
      xmlns:s="library://ns.adobe.com/flex/spark"
      xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="1280" minHeight="800" backgroundColor="#E7E7E7" creationComplete="init()">

 <fx:Script>
  <![CDATA[
   import mx.charts.chartClasses.DataDescription;
   import mx.collections.ArrayCollection;
   import mx.collections.Sort;
   import mx.controls.Alert;
   import mx.controls.CheckBox;
   import mx.controls.Image;
   import mx.controls.Label;
   import mx.core.UIComponent;
   import mx.events.CloseEvent;
   import mx.events.ColorPickerEvent;
   import mx.events.DataGridEvent;
   import mx.events.FlexEvent;
   import mx.events.ListEvent;
   import mx.managers.PopUpManager;
   import mx.managers.SystemManager;
   import mx.messaging.channels.StreamingAMFChannel;
   
   import org.osmf.layout.AbsoluteLayoutFacet;
   
   import vo.NetClass;
   import vo.Uinfo;
   import vo.Vpos;
   
   SharedObject.defaultObjectEncoding  = flash.net.ObjectEncoding.AMF0;
   private var soApp:String="rtmp://10.10.10.10/liveMeeting";//so服务器
   private var infoso:SharedObject;//存放用户列表等
   private var msgso:SharedObject;//存放实时聊天消息
   private var mu:Uinfo;
   
   [Bindable]
   public var resX:int = Capabilities.screenResolutionX;
   [Bindable]
   public var resY:int = Capabilities.screenResolutionY;  
   //显示视频大小,根据resX与resy值而变
   private var vpWidth:int;
   private var vpHeight:int;
   
   private var tracei:int=0;
   [Bindable]
   private var luserlist:ArrayCollection=new ArrayCollection();
   private var mymicstatus:Boolean=true;
   private var micloopback:Boolean=true;
   private var sonc:NetConnection;
   //记录视频窗口显示的位置等信息Vpos.class
   private var vpos:ArrayCollection=new ArrayCollection();
   
   //初始化vpWidth与vpHeight等参数(根据resX与resy值
   private function init():void
   {
    if (resX<1024)
    {
     Alert.show("<1024,error");
    }
    else if( resX<1280)
    {
     vpWidth=240;
     //vpHeight=180;
     vpHeight=200;
     Alert.show("<1280");
    }
    else if (resX<1660)
    {
     vpWidth=280;
     //vpHeight=210;
     vpHeight=230;
     
     Alert.show("<1660");
    }
    else if (resX<1920)
    {
     Alert.show("<1920");
    }
    else
    {
     vpWidth=320;
     //vpHeight=240;
     vpHeight=260;
     Alert.show(">=1920");
    }
    soconnect();
   }
   //so连接
   private function soconnect():void
   {
    sonc=new NetConnection();
    sonc.addEventListener(NetStatusEvent.NET_STATUS,sonetStatusHandler);
    sonc.connect(soApp);
    //Alert.show(resX.toString()+"X"+resY.toString());
   }
   //so连接
   private function sonetStatusHandler(evt:NetStatusEvent):void
   {
    trace(evt.info.code);  //调试代码用    
    if ( evt.info.code =="NetConnection.Connect.Success" )
    {
     infoso = SharedObject.getRemote("infoso",sonc.uri,true);
     infoso.addEventListener(SyncEvent.SYNC,infoSoSyncHandler);
     infoso.connect(sonc);     
     //doinfoSouserlist("add");
    }
    else
    {
     Alert.show("链接失败,出错代码:\n"+evt.info.code);
    }    
   }
   
   //移除某一用户
   private function removeItem(str:String,removestr:String):String
   {
    var arr:ArrayCollection=new ArrayCollection();
    arr=convertStringtoArray(str);
    //Alert.show(arr.length.toString(),"arrlength");
    //Alert.show(str,"oldstring");
    for (var i:uint;i<arr.length;i++)
    {
     var uo:Uinfo;
     uo=arr.getItemAt(i) as Uinfo;
     //Alert.show(uo.username+"=="+removestr);
     if (uo.username==removestr)
     {
      arr.removeItemAt(i);
      break;
     }
    }
    //Alert.show(arr.length.toString(),"arrlength after");
    str=convertArraytoString(arr);
    //Alert.show(str,"removedstring");
    return str;
   }
   //转化Uinfo Array to字符串格式
   private function convertArraytoString(arr:ArrayCollection):String
   {
    var str:String="";
    //Alert.show(str,"in convertArraytoString");
    for (var i:uint;i<arr.length;i++)
    {
     var uo:Uinfo=arr.getItemAt(i) as Uinfo;
     //Alert.show(uo.username+"/"+uo.userpriv+"/"+uo.MICstatus);
     str=str+uo.username+"|"+uo.userpriv+"|"+uo.MICstatus.toString()+"\n";
    }
    return str;
   }
   //转化字符串到Array (Uinfo格式),字符串格式为username|userpriv|Micstatus\n
   private function convertStringtoArray(ostr:String):ArrayCollection
   {
    //字符串最行都要求有\n标记
    //Alert.show(str.indexOf("\n",0).toString());
    //var str:String="111|efg|000\n222|eee|aaaa\n333|mmm|dddd\n";
    //var mark:String="\n";
    var arr:ArrayCollection=new ArrayCollection;
    arr.removeAll();
    var mark:String="\n";
    var markl:String="|";
    var str:String=ostr;
    //str="111|efg|000.222|eee|aaaa.333|mmm|dddd.";
    if( str.length<1)
     return null;
    var ui:uint=str.indexOf(mark,0);
    var oi:uint;
    //Alert.show(ui.toString());
    while( ui>0)
    {
     var lstring:String=str.substr(0,ui);
     var uo:Uinfo=new Uinfo();
     
     oi=lstring.indexOf(markl,0);
     uo.username=lstring.substr(0,oi);
     lstring=lstring.substr(oi+1);
     
     oi=lstring.indexOf(markl,0);
     uo.userpriv=lstring.substr(0,oi);
     if(lstring.substr(oi+1)=="true")
      uo.MICstatus=true;
     else
      uo.MICstatus=false;
    
     
     arr.addItem(uo);
    
     if(str.length<=ui+1)
     {
      break;
     }
      
     str=str.substr(ui+1);
     ui=str.indexOf(mark,0);
     
    }
    
    return arr;
    
   }
   //处理so同步事件
   private function infoSoSyncHandler(evt:SyncEvent):void
   {
    //Alert.show(infoso.data.userlist,"in syncHandler");
    if (luserlist==null)//第一次连接,初始化luserlist
    {
     luserlist=convertStringtoArray(infoso.data.userlist);
    }
    //Alert.show(convertArraytoString(luserlist)+"\n\n"+infoso.data.userlist,"luserlist onsyn");
    var nuserlist:ArrayCollection=new ArrayCollection();
    nuserlist=convertStringtoArray(infoso.data.userlist);
    var ni:uint=0;
    var vni:Uinfo;
    if (convertArraytoString(luserlist)!=infoso.data.userlist)
    {
     //Alert.show("changed","in syncHandler");
    //增加
     //Alert.show(nuserlist.length.toString()+"|"+luserlist.length.toString(),"n + luserlist.length");
     if (nuserlist.length>luserlist.length)
     {
      var ui:Uinfo=nuserlist.getItemAt(nuserlist.length-1) as Uinfo;
      //vpos.addItemAt(ui.username,nuserlist.length - 1);
      
      //处理视频,增加视频显示
      //轮询vpos,看哪个位置有空
      
      for(ni;ni<vpos.length;ni++)
      {
       var nvp:Vpos=vpos.getItemAt(ni) as Vpos;
       var us:String=nvp.uname;
       if(us==null)
       {
        //vpos.setItemAt(ui.username,ni);
        nvp.uname=ui.username;
        vpos.setItemAt(nvp,ni);
        break;
       }
      }
      //Alert.show(ni.toString(),"ni");
      if (ni>=vpos.length) //如果vpos中无空
      {
       var nnvp:Vpos= new Vpos;
       nnvp.uname=ui.username;
       vpos.addItem(nnvp);
      }
      //显示视频
      showvideo(ni);
      //更新luserlist
      luserlist=convertStringtoArray(infoso.data.userlist);
      
     }
     else if(nuserlist.length<luserlist.length) //减少
     {
      //设置vpos(i)为null,更新luserlist,释放vdplay
      for(ni;ni<nuserlist.length;ni++)
      {
       vni=nuserlist.getItemAt(ni) as Uinfo;
       var lni:Uinfo=luserlist.getItemAt(ni) as Uinfo;
       if (vni.username!=lni.username)
       {
        vposremove(lni.username);
        break;
       }
      }
      luserlist=convertStringtoArray(infoso.data.userlist);
     }
     else   //对其它客户端,MICstatus状态改变
     {
      for(ni;ni<nuserlist.length;ni++)
      {
       vni=nuserlist.getItemAt(ni) as Uinfo;
       //Alert.show(vnii.username + "|" + userName,"abab");
       if (vni.username==userName)
       {
        if(vni.MICstatus!=mymicstatus)//改变自己MIC状态
        {
         setMyMIC(vni.MICstatus);
         mymicstatus=vni.MICstatus;
        }
        
        break;
       } 
      }
      luserlist=convertStringtoArray(infoso.data.userlist);
     }
    }
    else //本客户端,luserlist起先已同步了userlist
    {
     //Alert.show("userchange","aa");
     for(ni;ni<nuserlist.length;ni++)
     {
      vni=nuserlist.getItemAt(ni) as Uinfo;
      //Alert.show(vnii.username + "|" + userName,"abab");
      if (vni.username==userName)
      {
       Alert.show(vni.MICstatus.toString(),"myMicstatus");
       break;
      } 
     }
    }
   }
   private function setMyMIC(onoroff:Boolean):void
   {
    //microphone.
    if (onoroff==false)
    {
     microphone.gain=0;
    }
    else
    {
     microphone.gain=100;
    }
    Alert.show(microphone.gain.toString(),"my mic gain");
   }
   private function vposremove(uname:String):void
   {
    for(var ni:uint;ni<vpos.length;ni++)
    {
     var nvp:Vpos=vpos.getItemAt(ni) as Vpos;
     if(nvp.uname==uname)
     {
      nvp.vp.source="";
      vpos.setItemAt(null,ni);
      break;
     }
    }
   }
   
   
   
   private var liveApp:String="rtmp://10.10.10.10/live";
   //发布流
   private var ns:NetStream;
   //链接fms
   private var nc:NetConnection;
   private var netcls:ArrayCollection=new ArrayCollection();
   private var userName:String;
   private var camera:Camera;   
   private var microphone:Microphone;
   private function onBWDone():void
   {
   }
   //发布自己视频
   private function pubMyVideo():void
   {
    nc= new NetConnection();
    nc.connect(liveApp);
    nc.client=this;
    nc.addEventListener(NetStatusEvent.NET_STATUS,onNetStatusHandler);
    //soconnect();    
   }
   private function onNetStatusHandler(evt:NetStatusEvent):void
   {
    trace(evt.info.code);
    if (evt.info.code == "NetConnection.Connect.Success")
    {
     ns = new NetStream(nc);
     ns.addEventListener(NetStatusEvent.NET_STATUS,onNetStatusHandler);
     ns.client=new  CustomClient();     
     ns.attachCamera(camera);
     ns.attachAudio(microphone);
     ns.publish(userName,"live");//发布本人视频
     showluservideo();
    }
   }
   
   private function showvideo(pi:int):void //根据vpos(i)的内容在pi位置显示视频
   {
    var vpName:VideoPlayer=new VideoPlayer;
    vpName.width=vpWidth;
    vpName.height=vpHeight;
    var uname:String;//=vpos.getItemAt(pi) as String;
    var nvp:Vpos=new Vpos;
    nvp=vpos.getItemAt(pi) as Vpos;
    uname=nvp.uname;
    
    vpName.source=liveApp+uname;
    vpName.styleName="vpskin.vpskin";
    //显示位置
    vpName.x=(pi%3)*vpWidth;
    if(pi>=3)
    {
     vpName.y=(int(pi/3))*(vpHeight-25);
    }
    else
    {
     vpName.y=(int(pi/3))*vpHeight;
    }       
    //Alert.show("i:"+i.toString()+"pi:"+pi.toString()+"pi%3:"+(pi%3).toString()+vpName.x.toString(),"for");
    var tf:TextFormat = new TextFormat();    
    var lbname:Label=new Label;
    lbname.text=nvp.uname;
    //vpName.addChild(lbname);
    lbname.x=vpName.x;
    lbname.y=vpName.y;
    tf.size=24;
    tf.color = 0xFFFFFF;
    lbname.setStyle("textFormat",tf);
    var lbname1:Label=new Label;
    lbname1.text=nvp.uname;
    lbname1.x=vpName.x+1;
    lbname1.y=vpName.y+12;
    tf.color = 0xFF0000;
    tf.color = 0x00FF00;
    lbname1.setStyle("textFormat", tf);
    
    
    gr1.addElement(vpName);
    gr1.addElement(lbname1);
    gr1.addElement(lbname);
    nvp.vp=vpName;
    
    vpos.setItemAt(nvp,pi);
    
   }
   //show luserlist's video显示luserlist中视频,自己不显示
   private function showluservideo():void
   {
    var pi:uint=0;
    vpos.removeAll();
    //Alert.show("in ","in showluser");
    for(var i:uint;i<luserlist.length;i++)
    {
     //Alert.show(i.toString(),"in showluser");
     var ui:Uinfo=luserlist.getItemAt(i) as Uinfo;
     if (ui.username == userName)//自己不显示
     {
      continue;
     }
     var nvp:Vpos=new Vpos;
     nvp.uname=ui.username;
     vpos.addItem(nvp);
     showvideo(pi);   
     pi++;
    }
   }
   
   //初始化多媒体设备
   private function initMedia():void
   {  
    this.setCam();
    this.setMic();
   }
   
   //设置摄像头 videoDisplay必须置于Container中,如<s:Group></s:Group>
   private function setCam():void
   {    
    camera= Camera.getCamera();
    
    if (camera)
    {
     //Alert.show(camera.name);
    }     
    else
    {
     Alert.show("摄像头错误!");
    }
    camera.setMode(320,240,25);
    //camera.setQuality(0,80);
    camera.setLoopback(true);
    camera.setQuality(0,100);
    var localCam:Video=new Video(320,240);
    localCam.attachCamera(camera);   
    vdMe.addChild(localCam);
   }
   
   //设置麦克风
   private function setMic():void
   {
    microphone=Microphone.getMicrophone();

   }
   //一连接SO,检测userlist列表中的用户,显示列表中用户视频流
   //发布自己的视频,更新SO。userlist,增加自己,同步事件中更新本地luserlist
   
   protected function btn_Connect_clickHandler(event:MouseEvent):void
   {
    this.initMedia();
    userName=textinput_uname.text;
    txtmsg.text="";
    pubMyVideo();
    adduser(userName);
   }
   //向userlist中增加自己,如果原列表中有,则不增加
   private function adduser(uname:String):void
   {
    for(var ni:uint;ni<luserlist.length;ni++)
    {
     var ui:Uinfo=luserlist.getItemAt(ni) as Uinfo;
     if(ui.username==uname)//检查原luserlist中是否已存在自己的名字
      return;
    }
    var nui:Uinfo=new Uinfo;
    nui.username=uname;
    nui.userpriv="主持人";
    nui.MICstatus=true;
    luserlist.addItem(nui);    
    infoso.setProperty("userlist",convertArraytoString(luserlist));   
   }
   
   

   //removeme from so test
   protected function removeme_clickHandler(event:MouseEvent):void
   {
    var str:String=new String();
    //str=infoso.data.userlist;
    str=removeItem(infoso.data.userlist as String,textinput_uname.text);
    //removeItem(str,textinput_uname.text);
    Alert.show(str,"removedstring");
    infoso.setProperty("userlist",str);
    
   }

   
   
   //show luserlist 改变MIC状态,更新userlist
   protected function btn_changemic_clickHandler(event:MouseEvent):void
   {    
    infoso.setProperty("userlist",convertArraytoString(luserlist));    
   }

   private function onchangeMIC(event:DataGridEvent):void
   {
    //Alert.show(convertArraytoString(luserlist),"onitemend");
   }   
   //开关MIC本地回响
   protected function btn_localaudio_clickHandler(event:MouseEvent):void
   {
    if (micloopback==true)
    {
     microphone.setLoopBack(false);
     btn_localaudio.label="本地回响(关)";
     micloopback=false;
    }
    else
    {
     microphone.setLoopBack(true);
     btn_localaudio.label="本地回响(开)";
     micloopback=true;
    }
   }

  ]]>
 </fx:Script>

 <fx:Declarations>
  <!-- 将非可视元素(例如服务、值对象)放在此处 -->
  
 </fx:Declarations>
 <s:TextArea x="10" y="438" width="320" id="txtmsg" height="111"/>
 <s:TextInput x="11" y="557" width="80" id="textinput_uname" text="admin"/>
 <s:TextInput x="102" y="557" width="154" id="textinput_message"/>
 <s:Button x="264" y="558" label="发送"  id="btn_submit"/>
 <mx:DataGrid x="10" y="293" width="320"
     dataProvider="{luserlist}" 
     editable="true"
     itemEditEnd="onchangeMIC(event)"  
      sortableColumns="false"
      
     >
  <mx:columns>
   <mx:DataGridColumn headerText="用户名" dataField="username" editable="false"/>    
   <mx:DataGridColumn headerText="名称" dataField="userpriv" editable="false"/>
   <mx:DataGridColumn headerText="发言" dataField="MICstatus"
          editable="true"
          rendererIsEditor="true"         
          itemRenderer="mx.controls.CheckBox"
          editorDataField="selected"
         
          >    

   </mx:DataGridColumn>
   
   
   
  </mx:columns>
 </mx:DataGrid>
 <s:Group x="345" y="10" width="853" height="640" id="gr1">
  <s:VideoPlayer x="592" y="422" width="261" height="100%" dropShadowVisible="true" id="vp1" skinClass="vpskin.vpskin"/>
 </s:Group>
 <s:Button x="10" y="264" label="连接(测试)" id="btn_Connect" click="btn_Connect_clickHandler(event)"/>
 <s:Group x="10" y="10" width="320" height="240" id="skc">
  <s:VideoDisplay x="-1" y="0" width="320" height="240" id="vdMe"/>
 </s:Group>
 <s:Button x="66" y="591" label="removeme" id="removeme" click="removeme_clickHandler(event)"/>
 <s:Button x="213" y="263" label="发送命令" click="btn_changemic_clickHandler(event)" id="btn_changemic"/>
 <s:Button x="110" y="264" label="本地声音(开)" id="btn_localaudio" click="btn_localaudio_clickHandler(event)"/>
 
 
 
 
</s:Application>

Uinfo类

package vo
{
[RemoteClass(alias="Uinfo")]

public class Uinfo
{
 public function Uinfo()
 {
 }
 public var username:String;
 public var userpriv:String;
 //public var logintime:String;
 //public var MICstatus:String; 
 public var MICstatus:Boolean;
 
}
}


Vpos类

package vo
{
 import spark.components.VideoDisplay;
 import spark.components.VideoPlayer;

 public class Vpos
 {
  public function Vpos()
  {
  }
  public var uname:String;
  public var vp:VideoPlayer;
  
 }
}



  评论这张
 
阅读(492)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017