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

by Peter deHaan on January 22, 2008

in Camera,VideoDisplay

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>

{ 8 comments… read them below or add one }

1 Anonymous January 23, 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 :- )

Reply

2 Andrew Paul Simmons May 29, 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!

Reply

3 Beth August 11, 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.

Reply

4 Beth August 11, 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!

Reply

5 Wins January 16, 2009 at 11:38 pm

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.

Reply

6 Peter deHaan January 17, 2009 at 8:38 pm
7 Dan July 3, 2009 at 4:13 am

It appears this doesn’t work on firefox 3.5,

camera.addEventListener(StatusEvent.STATUS, camera_status);

this event is not being called

Reply

8 Gege May 24, 2010 at 4:50 pm

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

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: