22
Jan
08

Detecting changes in a camera’s activity and status in a Flex VideoDisplay control

In a previous example, “Displaying a webcam’s video in a Flex VideoDisplay control”, we saw how to connect to a user’s webcam, if they have one installed.

In the following example we see how to listen for the Camera object’s activity event and status event, using some good ol’ ActionScript.

Full code after the jump.

This example won’t be very interesting unless you actually have a webcam installed.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/22/detecting-changes-in-a-cameras-activity-and-status-in-a-flex-videodisplay-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="horizontal"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.utils.StringUtil;

            private function videoDisplay_creationComplete():void {
                var camera:Camera = Camera.getCamera();
                if (camera) {
                    videoDisplay.attachCamera(camera);
                    camera.addEventListener(ActivityEvent.ACTIVITY, camera_activity);
                    camera.addEventListener(StatusEvent.STATUS, camera_status);
                } else {
                    Alert.show("You don't seem to have a camera.");
                }
            }

            private function camera_activity(evt:ActivityEvent):void {
                var str:String = "[{0}] activating:{1}\n";
                textArea.text += StringUtil.substitute(str,
                                    evt.type,
                                    evt.activating);
            }

            private function camera_status(evt:StatusEvent):void {
                var str:String = "[{0}] code:'{1}', level:'{2}'\n";
                textArea.text += StringUtil.substitute(str,
                                    evt.type,
                                    evt.code,
                                    evt.level);
                switch (evt.code) {
                    case "Camera.Muted":
                        Alert.show("User denied access to camera.");
                        break;
                    case "Camera.Unmuted":
                        Alert.show("User allowed access to camera.");
                        break;
                }
            }
        ]]>
    </mx:Script>

    <mx:VideoDisplay id="videoDisplay"
            creationComplete="videoDisplay_creationComplete();"
            width="160"
            height="120" />

    <mx:TextArea id="textArea"
            editable="false"
            width="100%"
            height="{videoDisplay.height}"
            wordWrap="false"
            verticalScrollPolicy="on" />

</mx:Application>

View source is enabled in the following example.

If you want to display the Adobe Flash Player Settings dialog box again, you can use the following ActionScript:

Security.showSettings(SecurityPanel.PRIVACY);

You can also allow users to go to different tabs in the Settings dialog by by using something similar to the following snippet:

<mx:ApplicationControlBar dock="true">
    <mx:Button label="Camera"
            click="Security.showSettings(SecurityPanel.CAMERA);" />
    <mx:Button label="Privacy"
            click="Security.showSettings(SecurityPanel.PRIVACY);" />
</mx:ApplicationControlBar>

4 Responses to “Detecting changes in a camera's activity and status in a Flex VideoDisplay control”


  1. 1 Anonymous Jan 23rd, 2008 at 1:38 pm

    Hi i have a question how do you save / record a video from a web cam in flex :-) your examples are good :- )

  2. 2 Andrew Paul Simmons May 29th, 2008 at 4:04 pm

    Switching from an active camera to a camera that is not present and then switching back to the clearly active camera appears to cause the activity level to remain at -1!

  3. 3 Beth Aug 11th, 2008 at 10:14 am

    This example doesn’t work with the built-in camera on my Mac Book Pro. Do you have any idea why that is?

    The status information is good (code:’Camera Unmuted’), but then I never get any activity messages, and the image stays black. I’m trying to get my own code working (which does something similar) and having the same problem.

  4. 4 Beth Aug 11th, 2008 at 10:30 am

    I solved my own problem. The fix is: right click on the SWF, then Settings, then change the camera to a USB device. Now it works!

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".