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>
 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

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

  1. Anonymous says:

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

  2. 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. Beth says:

    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. Beth says:

    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!

  5. Wins says:

    This doesn’t work on my Macbook pro running vista either.
    I can’t get picture to display and the green light next to my web cam never turns on.
    I have a flash recorder program on my own website and my web cam works on that.
    So I’m not sure why it’s not working here.

  6. Dan says:

    It appears this doesn’t work on firefox 3.5,

    camera.addEventListener(StatusEvent.STATUS, camera_status);

    this event is not being called

  7. Gege says:

    Hello

    This example is not working for me :S

    Try it under Flex 3, Adobe Builder 4 and try it in IE and Firefox :S

    Any suggestion?

    Regards
    G

  8. pankaj says:

    Hi peter,

    How can i connect flash builder with an ip camera ?

    Please help me out. need this to implement ASAP.

    Thanks

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree