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.
<?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 }
Hi i have a question how do you save / record a video from a web cam in flex :-) your examples are good :- )
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!
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.
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!
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.
Wins,
Go here; http://blog.flexexamples.com/2008/01/25/getting-specific-camera-instances-using-the-static-cameragetcamera-method/
How many cameras does your MacBook show as having?
Peter
It appears this doesn’t work on firefox 3.5,
camera.addEventListener(StatusEvent.STATUS, camera_status);
this event is not being called
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