Programming ActionScript 3.0 |
|
|
|
| Working with video > Capturing camera input > Detecting permissions for camera access | |||
Before the camera's output can be displayed, the user must explicitly allow Flash Player to access the camera. When the attachCamera() method gets called Flash Player displays the Flash Player Settings dialog box which prompts the user to either allow or deny Flash Player access to the camera and microphone. If the user clicked the Allow button, the camera's output is displayed in the Video instance on the Stage. If the user clicked the Deny button, Flash Player is unable to connect to the camera and the Video object will not display anything.
If the user does not have a camera installed, Flash Player will display nothing. If the user does have a camera installed, Flash Player will display the Flash Player Settings dialog which prompts the user to either allow or deny Flash Player to access the Camera. If the user allows access to their camera the video will be displayed back to the user, otherwise nothing will be displayed.
If you want to detect whether the user allowed or denied access to the camera, you can listen for the camera's status event (StatusEvent.STATUS), as seen in the following code:
var cam:Camera = Camera.getCamera();
if (cam != null)
{
cam.addEventListener(StatusEvent.STATUS, statusHandler);
var vid:Video = new Video();
vid.attachCamera(cam);
addChild(vid);
}
function statusHandler(event:StatusEvent):void
{
// This event gets dispatched when the user clicks the "Allow" or "Deny"
// button in the Flash Player Settings dialog box.
trace(event.code); // "Camera.Muted" or "Camera.Unmuted"
}
The statusHandler() function gets called as soon as the user clicks either Allow or Deny. You can detect which button the user clicked, using one of two methods:
event parameter of the statusHandler() function contains a code property which contains the string "Camera.Muted" or "Camera.Unmuted". If the value is "Camera.Muted" the user clicked the Deny button and Flash Player is unable to access the camera. You can see an example of this in the following snippet:
function statusHandler(event:StatusEvent):void
{
switch (event.code)
{
case "Camera.Muted":
trace("User clicked Deny.");
break;
case "Camera.Unmuted":
trace("User clicked Accept.");
break;
}
}
muted which specifies whether the user has denied access to the camera (true) or allowed access (false) in the Flash Player Privacy panel. You can see an example of this in the following snippet:
function statusHandler(event:StatusEvent):void
{
if (cam.muted)
{
trace("User clicked Deny.");
}
else
{
trace("User clicked Accept.");
}
}
By checking for the status event to be dispatched, you can write code that handles the user accepting or denying access to the camera and clean up appropriately. For example, if the user clicks the Deny button, you could display a message to the user stating that they need to click Allow if they want to participate in a video chat, or you could instead make sure the Video object on the display list is deleted to free up system resources.
|
|
|
|