14.4. Capturing and Displaying Video from a Web Cam

Problem

You want to display the video from a user’s web cam within a Flash movie, or you want to capture the data in order to publish it to a FlashCom application. (See Recipe 14.9.)

Solution

Use the Camera.get( ) method.

Discussion

The Camera.get( ) method returns a stream of (video-only) data from the camera attached to the user’s computer. For example:

myCam_cam = Camera.get(  );

If there are multiple cameras, Flash defaults to the camera that is selected under the Camera tab of the Player Settings dialog box. Flash also assigns each recognized camera an index, starting from 0 and incrementing by 1. For example, if there are three cameras attached to the computer, Flash identifies them by the indexes 0, 1, and 2. You can pass the index to the Camera.get( ) method to open a data stream from that camera:

// Open a stream from the second camera.
myCam_cam = Camera.get(1);

In practice, developers ordinarily use one of two strategies:

  • Allow the user to select a camera before opening the camera stream.

  • Open the camera stream to the default camera, and then allow the user to choose a different camera.

You can allow a user to choose a camera in at least two ways:

  • Open the Player Settings dialog box to the Camera tab using the following code:

    System.showSettings(3);

    However, this technique is intrusive because it confronts the user with the Player Settings dialog box. Therefore, you should provide a button to open the Player Settings dialog box, ...

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.