flex视音频通讯-摄像头及麦克风检测

<mx:Script>
<![CDATA[
import mx.controls.VideoDisplay;
import flash.media.Camera;
import flash.media.Microphone;
import mx.controls.Alert; 
import mx.events.CloseEvent; 
import mx.rpc.events.ResultEvent; 
import mx.rpc.events.FaultEvent;
import mx.utils.StringUtil;
import mx.managers.PopUpManager;
import flash.net.NetConnection;
import mx.rpc.soap.mxml.WebService;
import mx.collections.ArrayCollection;

[Bindable]
private var camData:ArrayCollection = new ArrayCollection();
[Bindable]
private var micData:ArrayCollection = new ArrayCollection();

private var mycamera:Camera;
private var mymicrophone:Microphone; 
private var isCamera:Boolean=false;
private var isMicrophone:Boolean=false;
private var isMicrophone2:Boolean=false; 
private var iserrmsg:Boolean=false; 
private var showtimer:Timer=new Timer(100);

NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;

private function login_init():void
{

camData.removeAll();
for(var i:String in Camera.names)
{
var item:Object={data:i,Lable:Camera.getCamera(i).name.toString()};
camData.addItem(item);
}
micData.removeAll();
for(var j:String in Microphone.names)
{
var item2:Object={data:i,Lable:Microphone.getMicrophone(Number(j)).name.toString()};
micData.addItem(item2);
} 
Cam_init();
Mic_init(); 
}
private function Cam_init():void
{
isCamera=false; 
cam_lbl.text="" ;
var index:int=cam_CB.selectedIndex;
if(index>-1)
{
mycamera = Camera.getCamera(index.toString());
}
else
{
mycamera = Camera.getCamera();
}
if(mycamera != null)
{
mycamera.addEventListener(StatusEvent.STATUS,camonStatusHandler);
mycamera.addEventListener(ActivityEvent.ACTIVITY, camactivityHandler);
mycamera.setMode(240,180,12);//设置所捕获视频的宽度(160)高度(120)帧速(15)是否控制宽度、高度和帧频(true)
mycamera.setQuality(0,85);//设置输出视像输送的最大带宽(max:0,default:16384)以及画面质量(max:0,1-100)。
mycamera.setKeyFrameInterval(48);//设置视频传输的帧速和压缩算法(1-48)。
var camvd:VideoDisplay = new VideoDisplay();
camvd.width=240;
camvd.height=180;
vd_canvas.addChild(camvd);
camvd.attachCamera(mycamera);
cam_lbl.text="摄像头未接入或被占用";
}
else
{
isCamera=false; 
cam_lbl.text="摄像头未安装";
}
}
//摄像头初始化返回结果
private function camactivityHandler(event:ActivityEvent):void
{
isCamera=true;
cam_lbl.text="摄像头工作正常";
mycamera.removeEventListener(ActivityEvent.ACTIVITY,camactivityHandler);
}
//检测摄像头允许使用权限
private function camonStatusHandler(event:StatusEvent):void
{
if(mycamera.muted)
{
isCamera=false;
cam_lbl.text="摄像头未允许使用";
}
else
{
mycamera.removeEventListener(StatusEvent.STATUS,camonStatusHandler);
}
}
private function Mic_init():void
{
isMicrophone=false;
isMicrophone2=false;
mic_lbl.text="";
var index:int=mic_CB.selectedIndex;
if(index>-1)
{
mymicrophone = Microphone.getMicrophone(index);
}
else
{
mymicrophone = Microphone.getMicrophone();
}
mymicrophone.setUseEchoSuppression(true);
mymicrophone.setLoopBack(true);
if(mymicrophone != null)
{
showtimer.addEventListener(TimerEvent.TIMER,micvolshow_show); 
showtimer.start(); 
mymicrophone.addEventListener(ActivityEvent.ACTIVITY, micactivityHandler);
mymicrophone.addEventListener(StatusEvent.STATUS, micstatusHandler);
isMicrophone=true; 
mic_lbl.text="麦克风未接入或被占用";
}
else
{
isMicrophone=false;
mic_lbl.text="麦克风未安装";
}
}
private function micactivityHandler(event:ActivityEvent):void 
{
isMicrophone=true;
isMicrophone2=true;
mic_lbl.text="麦克风正常";
mymicrophone.removeEventListener(ActivityEvent.ACTIVITY,micactivityHandler);
}

private function micstatusHandler(event:StatusEvent):void
{
if(mymicrophone.muted)
{
isMicrophone=false;
mic_lbl.text="麦克风未允许使用";
}
else
{ 
isMicrophone=true;
mymicrophone.removeEventListener(StatusEvent.STATUS,micstatusHandler);
} 
}
private function micvolshow_show(event:TimerEvent):void
{
audioshow_bar.width=int(mymicrophone.activityLevel);
}

]]>
</mx:Script>
<mx:Style source="ico/body.css"></mx:Style>
<mx:Label x="262" y="43" text="摄像头状态:" color="#000000"/>
<mx:Label x="262" y="148" text="麦克风状态:" color="#000000"/>
<mx:Label x="10" y="259" width="426" textAlign="center" id="conn_lbl" color="#FF0000"/>
<mx:Label x="262" y="62" width="178" id="cam_lbl" color="#FF0000" text="没有图像哦"/>
<mx:Label x="262" y="169" width="178" id="mic_lbl" color="#FF0000" text="没有声音哦"/>
<mx:Canvas x="10" y="9" width="240" height="180" backgroundImage="@Embed(source='ico/videobj.png')" id="vd_canvas">
</mx:Canvas>
<mx:ComboBox x="258" y="10" id="cam_CB" width="178" dataProvider="{camData}" labelField="Lable" change="Cam_init();"></mx:ComboBox>
<mx:ComboBox x="258" y="91" id="mic_CB" width="178" dataProvider="{micData}" labelField="Lable" change="Mic_init();"></mx:ComboBox>

 

你可能感兴趣的:(算法,webservice,css,Flex,Flash)