Programming ActionScript 3.0 |
|
|
|
| Working with video > Capturing camera input > Verifying that cameras are installed | |||
Before you attempt to use any methods or properties on a camera instance, you'll want to verify that the user has a camera installed. There are two ways to check whether the user has a camera installed:
Camera.names property which contains an array of camera names which are available. Typically this array will have one or fewer strings, as most users will not likely have more than one camera installed at a time. The following code demonstrates how you could check the Camera.names property to see if the user has any available cameras:
if (Camera.names.length > 0)
{
trace("User has no cameras installed.");
}
else
{
var cam:Camera = Camera.getCamera(); // Get default camera.
}
Camera.getCamera() method. If no cameras are available or installed, this method returns null, otherwise it returns a reference to a Camera object. The following code demonstrates how you could check the Camera.getCamera() method to see if the user has any available cameras:
var cam:Camera = Camera.getCamera();
if (cam == null)
{
trace("User has no cameras installed.");
}
else
{
trace("User has at least 1 camera installed.");
}
Since the Camera class doesn't extend the DisplayObject class, it cannot be directly added to the display list using the addChild() method. In order to display the camera's captured video, you need to create a new Video object and call the attachCamera() method on the Video instance.
This snippet shows how you can attach the camera if one exists; if not, Flash Player simply displays nothing:
var cam:Camera = Camera.getCamera();
if (cam != null)
{
var vid:Video = new Video();
vid.attachCamera(cam);
addChild(vid);
}
|
|
|
|