I built an FLAR and PV3D app and it is working well except I want to detect the state of the users camera. I either want to know if the camera is off or in use by a different program, or if they don’t have a camera installed. My attempts have been marginally successful, but don’t always work as expected, and don’t work at all if no camera driver is present at all.
See: http://www.danielbrian.com/christmas and http://www.danielbrian.com/christmas/test
How can I detect the aforementioned camera states?
This is what I have:
private function cameraCheck():void
{
trace("***:", Camera.getCamera(), Camera.names.length);
if (Camera.getCamera() == null || Camera.names.length == 0)
{
camName = "PV3D";
doTrackLoaded();
return;
}
var checkCam:Camera = Camera.getCamera();
var repeat:uint = 20;
camTimer = new Timer(50, repeat);
var vid:Video = new Video();
var camCount:Number = new Number();
vid.visible = false;
vid.attachCamera(checkCam);
addChild(vid);
//checkCam.addEventListener(StatusEvent.STATUS, statusEventHandler);
camTimer.addEventListener(TimerEvent.TIMER, timerCheck);
camTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
camTimer.start();
function statusEventHandler(se:StatusEvent):void
{
trace(se.code);
switch (se.code) {
case "Camera.Muted":
trace("User denied access to camera.");
break;
case "Camera.Unmuted":
trace("User allowed access to camera.");
/* checkCam.removeEventListener(StatusEvent.STATUS, statusEventHandler);
camTimer.removeEventListener(TimerEvent.TIMER, timerCheck);
camTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
camTimer.stop(); */
camName = "FLAR";
doTrackLoaded();
break;
}
}
function timerCheck(te:TimerEvent):void
{
camCount += checkCam.currentFPS;
// checks if the security panel is open
closed = true;
var dummy:BitmapData;
dummy = new BitmapData(1, 1);
try
{
// Try to capture the stage: triggers a Security error when the settings dialog box is open
dummy.draw(stage);
}
catch (error:Error)
{
closed = false;
camTimer.repeatCount ++;
trace(camTimer.repeatCount);
}
trace("security panel open: " + closed);
dummy.dispose();
dummy = null;
}
function timerComplete(te:TimerEvent):void
{
var camActive:Number = camCount / repeat;
if (camActive > 0)
{
cam = FLARcam;
camName = "FLAR";
}
else
{
cam = PV3Dcam;
camName = "PV3D";
addCameraOffMessage();
}
camTimer.removeEventListener(TimerEvent.TIMER, timerCheck);
camTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
removeChild(vid);
checkCam = null;
cameraCheckComplete = true;
}
}