[ Team LiB ] |
Recipe 14.4 Capturing and Displaying Video from a Web Cam14.4.1 ProblemYou 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 SolutionUse the Camera.get( ) method. 14.4.3 DiscussionThe 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:
You can allow a user to choose a camera in at least two ways:
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).
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 AlsoRecipe 14.5, Recipe 14.9, and the video chat application in Chapter 24 |
[ Team LiB ] |