0

I am attempting to utilise the front facing camera on an Android device using the code below. Permissions are set up correctly but it wont seem to work. When I remove CameraPosition.Front from getCamera it accesses the devices standard camera fine. Any suggestions ?

var cam:Camera = Camera.getCamera(CameraPosition.FRONT); 
var vid:Video = new Video(); 
vid.attachCamera(cam); 
addChild(vid);
if (cam != null) 
{ 
    cam.addEventListener(StatusEvent.STATUS, statusHandler); 
    vid = new Video(); 
    vid.attachCamera(cam); 
} 
function statusHandler(event:StatusEvent):void 
{ 
    if (!cam.muted) 
    { 
        vid.width = cam.width; 
        vid.height = cam.height; 
        addChild(vid); 

    } 
    cam.removeEventListener(StatusEvent.STATUS, statusHandler); 
} 

1 Answers1

3

That's not how to reference the camera. Use the position parameter of a Camera class instance to determine the position of the Camera.

An example:

var frontCamera:Camera = tryGetFrontCamera();
if (!frontCamera) {
    //Front facing camera unavailable
}

...

public function tryGetFrontCamera():Camera {
    var numCameras:uint = (Camera.isSupported) ? Camera.names.length : 0;
    for (var i:uint = 0; i < numCameras; i++) {
        var cam = Camera.getCamera(String(i));
        if (cam && cam.position == CameraPosition.FRONT) {
            return cam;
        }
    } 
    return null;
}

Documentation.

david.emilsson
  • 2,313
  • 1
  • 20
  • 24