DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 14.4 Capturing and Displaying Video from a Web Cam

14.4.1 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.)

14.4.2 Solution

Use the Camera.get( ) method.

14.4.3 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, which offers the user more control. For example:

    openSettings_btn.onRelease = function (  ) {
      System.showSettings(3);
    };
  • Create a custom UI, such as combo box, to allow the user to select his camera. You can populate the combo box using the Camera.names array, which contains the names of the available cameras.

    // Include Form.as from Chapter 11 to access its custom adjustWidth(  ) method.
    // This is optional, as it is for formatting purposes only.
    #include "Form.as"
    
    // For this example to work, you must add a combo box named cameraSelector_cb to
    // your movie.
    
    // Define a change handler function for the combo box.
    function selectCamera (cb) {
      // Create a camera object using the index of the camera the user has selected.
      selected_cam = Camera.get(cb.getValue(  ));
    }
    
    // Loop through all the elements of the Camera.names array.
    for (var i = 0; i < Camera.names.length; i++) {
      // Add the camera names to the combo box.
      cameraSelector_cb.addItem(Camera.names[i], i);
    }
    
    // Adjust the width of the combo box to accommodate the contents.
    cameraSelector_cb.adjustWidth(  );
    
    // Set the change handler function to selectCamera(  )
    cameraSelector_cb.setChangeHandler("selectCamera");

In isolation, the Camera.get( ) method is not very useful. You need to apply the camera data to a net stream for publishing to a FlashCom application (see Recipe 14.9) or to a video object within the same Flash movie. To apply the camera data to a video object, use the attachVideo( ) method. This is useful for allowing a user to see what his camera is displaying (perhaps while simultaneously publishing that same data to a FlashCom server).

The video displayed from a local web cam is, in most cases, of higher quality than the video published to the FlashCom server. Although the aforementioned technique of monitoring local web cam data is useful, the user should be warned that subscribers to the published stream will not necessarily see video of the same quality.

Some users might not have any camera connected to their computer. Therefore, you should test to see whether a camera exists before trying to get the camera stream. A simple way to check for a camera is to make sure that the Camera.names array has at least one element.

This example checks whether a camera exists before attempting to display the video:

// Make sure that the user has a camera.
if (Camera.names.length > 0) {
  // The user has a camera, so let's get and use the camera data source.
  myCam = Camera.get(  );

  // Apply the data to a video object, my_video, which is nested within myVideo_mc,
  // as covered in Recipe 14.3. This code displays the user's video output in a Video 
  // object within the same Flash movie.
  myVideo_mc.my_video.attachVideo(myCam);
} else {
  // The user doesn't have a video camera. 
  // Display an alert using a message box named alertBox.
  alertBox.setMessage("This application requires that you have a camera.");
  alertBox._visible = true;
}

Although it is possible to attach camera data to a video object (or a net stream) with an anonymous reference, it is usually better to first create a variable to which you assign the camera stream, and then pass that variable to the attachVideo( ) method. The reason for this is that sometimes, even after a stream has been removed from the embedded video or the net stream has stopped publishing, the camera might not be released properly by Flash. This is not necessarily a problem in terms of the functioning of the FlashCom application, but it can be a little strange when a user's web cam light is still on even after a FlashCom application has stopped using it. You can set the camera stream variable to null at any point to release the camera. If you have used an anonymous reference to the camera stream, this option isn't open to you.

This example passes an anonymous reference to a camera stream to attachVideo( ):

myVideo_mc.my_video.attachVideo(Camera.get(  ));

This example assigns a variable to the camera reference before passing it to attachVideo( ):

myCam = Camera.get(  );
myVideo_mc.my_video.attachVideo(myCam);
// At a later point, setting myCam (the camera stream variable) 
// to null releases the camera.
myCam = null;

If you are not concerned with Flash properly releasing the camera, there is nothing wrong with passing attachVideo( ) an anonymous reference to the camera.

14.4.4 See Also

Recipe 14.5, Recipe 14.9, and the video chat application in Chapter 24

    [ Team LiB ] Previous Section Next Section