Displaying webcams video feed in Flex VideoDisplay Component

Printer-friendly versionPrinter-friendly versionSend to friendSend to friend

This example shows you how to get access to the video feed from your computers webcam via the Flex VideoDisplay component. This caused me an hour of headscratching so hopefully this example will help some others out there!

Initially I tried creating a camera object using the following code:

import mx.controls.Alert;
import flash.media.Camera;
import mx.events.ListEvent;
import mx.utils.ObjectUtil;

private function get_camera():void{

    var camera:Camera = new Camera();
    
    if(!camera){
          Alert.show('No camera detected');		
    }else{
	  Alert.show(camera.name);
	  webcam_display.attachCamera(camera);
   }
}

This code did not work as expected, it gathered a list of cameras (three for some reason, even though I only have a built in iSight) so it knew something was available to use, but it would not display any video.

So I had a quick check of the AS3 Language reference and came across the static Camera.getCamera() method. Instead of creating a new camera object as in the code above, you need to make a very slight change and do it this way instead:

import mx.controls.Alert;
import flash.media.Camera;
import mx.events.ListEvent;
import mx.utils.ObjectUtil;

private function get_camera():void{
    //Change is made to this line!
    var camera:Camera = Camera.getCamera();
				
    if(Camera.names.length == 0){
        Alert.show('No camera detected');					
    }else{
       Alert.show(camera.name);
       webcam_display.attachCamera(camera);
    }
}

As you can see, we are not using an instance of the camera class here, just importing the Class and using the static method provided by the Language reference. As soon as I made this change I saw my mugshot on the screen, and all was good! I need to look into the docs a little more to see why I cannot create a camera object in the normal way and if I do dig anything up I will post it here but hopefully this will save some head scratching and / or swearing for someone out there.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Lines and paragraphs break automatically.
  • You may embed videos from the following providers . Just add the video URL to your textarea in the place where you would like the video to appear, i.e. http://www.youtube.com/watch?v=pw0jmvdh.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Insert Flickr images: [flickr-photo:id=230452326,size=s] or [flickr-photoset:id=72157594262419167,size=m].
  • You may use <swf file="song.mp3"> to display Flash files inline
  • Twitter-style @usersnames are linked to their Twitter account pages.
  • Twitter-style #hashtags are linked to search.twitter.com.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.