Developing Android Applications with Adobe AIR [75]
The first step is to verify that your device supports access to the camera by checking the CameraUI.isSupported property. Note that, as of this writing, Android does not support the front camera natively, and therefore neither does AIR:
import flash.media.CameraUI;
if (CameraUI.isSupported == false) {
trace("no camera accessible");
return;
}
If it is supported, create an instance of the CameraUI class.
Register your application to receive camera events. A MediaEvent.COMPLETE is dispatched after a picture is taken, an Event.CANCEL if no media is selected, and an ErrorEvent if there is an error in the process:
import flash.events.MediaEvent;
import flash.events.ErrorEvent;
import flash.media.CameraUI;
var cameraUI:CameraUI = new CameraUI();
cameraUI.addEventListener(MediaEvent.COMPLETE, onComplete);
cameraUI.addEventListener(Event.CANCEL, onCancel);
cameraUI.addEventListener(ErrorEvent.ERROR, onError);
Call the launch function and pass the type MediaType.VIDEO as a parameter. This will launch the camera in video mode automatically:
import flash.media.MediaType;
var cameraUI:CameraUI = new CameraUI();
cameraUI.launch(MediaType.VIDEO);
function onError(event:ErrorEvent):void {
trace(event.text);
}
The camera application is now active and in the foreground. The AIR application moves to the background and waits.
Once the event is received, the camera application automatically closes and the AIR application moves back to the foreground.
Video capture on Android requires a lot of memory. To avoid having the Activity Manager terminate the application, the capture setting is restricted to low resolution by default, which requires a smaller memory buffer.
WARNING
MPEG-4 Visual, Android low-resolution video, is not supported by AIR. Therefore, captured videos cannot be played back in AIR. The native application can be used to play back the recorded videos.
Currently, this functionality should only be used for capturing and not viewing unless you use the native application in the Gallery. The video is saved in a 3GP format that AIR does not support. Trying to play it back will just display a white screen.
In the following example, I provide the code for playback in AIR in case this is resolved in the future.
On select, a MediaEvent object is returned:
import flash.media.Video;
import flash.net.netConnection;
import flash.net.netStream;
var videoURL:String;
var connection:NetConnection;
function onComplete(event:MediaEvent):void {
videoURL = event.data.file.url;
connection = new NetConnection();
connection.addEventListener(NetStatusEvent.NET_STATUS, onStatus);
}
function onStatus(event:NetStatusEvent):void {
switch(event.info.code) {
case "NetConnection.Connect.Success" :
connectStream();
break;
case "NetStream.Play.StreamNotFound" :
trace("video not found " + videoURL);
break;
}
}
function connectStream():void {
stream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS, onStatus);
stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError);
var video:Video = new Video();
video.attachNetStream(stream);
stream.play(videoURL);
addChild(video);
}
function onAsyncError(event:AsyncErrorEvent):void {
trace("ignore errors");
}
The Camera Class
The device’s camera, using the flash.media.Camera class, can be attached to a Video object in the AIR application. You can use this approach to simulate a web cam or for an Augmented Reality project.
The hardware orientation of the camera is landscape, so try to make your application’s orientation landscape too by changing the aspectRatio tag in your application descriptor:
The setMode function is used to determine the video’s resolution:
import flash.media.Camera;
import flash.media.Video;
var camera:Camera = Camera.getCamera();
if (camera != null) {
camera.setMode(stage.stageWidth, stage.stageHeight, 15, true);
var