原理:摄像头激活后,持续检测与之关联的Video对象是否在播放(即每秒帧数)
代码语言:javascript复制btnCheck.addEventListener(MouseEvent.CLICK,btnCheckClick);
var cam:Camera;
var video:Video;
var intervalId:uint;
var intelvalTimes:uint=0;
function btnCheckClick(e:MouseEvent) {
cam=Camera.getCamera();
if (cam==null) {
lblResult.text="未安装摄像头!";
return;
}
cam.addEventListener(StatusEvent.STATUS, statusHandler);
cam.addEventListener(ActivityEvent.ACTIVITY,camActivityHandler);
video=new Video(cam.width,cam.height);
//trace("视频宽度:" cam.width ",视频高度:" cam.height);
video.x=10;
video.y=40;
video.attachCamera(cam);//执行这句时,flash才会弹出摄像头是否允许使用提示框
}
//摄像头有活动时,被触发
function camActivityHandler(e:ActivityEvent) {
trace(e);
intervalId=setInterval(checkCallBack,100);
}
function checkCallBack():void {
intelvalTimes =1;
trace(intelvalTimes);
if (cam.currentFPS>0) {
lblResult.text="摄像头工作正常!";
clearInterval(intervalId);
addChild(video);//加载到当前舞台中
} else {
if (intelvalTimes>=20) {//持续检测2秒,仍然无图象的话,认为"设备无法使用(占用中)"
lblResult.text="设备无法使用(有可能被占用)";
clearInterval(intervalId);
}
}
}
//用户选择"同意"或"不允许"使用摄像头时触发
function statusHandler(e:StatusEvent) {
trace(e);
if (e.code=="Camera.Muted") {
lblResult.text="您不允许使用摄像头!";
} else if (e.code == "Camera.Unmuted") {
lblResult.text="摄像头视频获取中...";
}
}