Capturing Video

1
121

The native video camera can be used to capture video within AIR.

Video and the CameraUI Class

You can use the native camera within AIR to capture video. Your application needs to have permission. In Flash Professional, select File→AIR Android settings→Permissions→ Camera. In Flash Builder, add the following permission:

[code]<uses-permission android:name=”android.permission.CAMERA”/>[/code]

The flash.media.CameraUI class is an addition to the ActionScript language to support the device’s native camera application. It is a subclass of the EventDispatcher class and is only supported on AIR for mobile.

This object allows you to launch the native camera application to shoot a video while your AIR application moves to the background.

When you use the native camera, it comes to the foreground, your AIR application moves to the background, and NativeApplication Event.DEACTIVATE is fired. Make sure you don’t have any logic that could interfere with the proper running of your application, such as exiting. Likewise, when the native camera application quits and your AIR comes back to the foreground, Event.ACTIVATE is called.

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:

[code]

import flash.media.CameraUI;
if (CameraUI.isSupported == false) {
trace(“no camera accessible”);
return;
}

[/code]

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:

[code]

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);

[/code]

Call the launch function and pass the type MediaType.VIDEO as a parameter. This will launch the camera in video mode automatically:

[code]

import flash.media.MediaType;
var cameraUI:CameraUI = new CameraUI();
cameraUI.launch(MediaType.VIDEO);
function onError(event:ErrorEvent):void {
trace(event.text);
}

[/code]

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.

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:

[code]

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”);
}

[/code]

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:

[code]<aspectRatio>landscape</aspectRatio>[/code]

The setMode function is used to determine the video’s resolution:

[code]

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 video:Video = new Video(camera.width, camera.height);
video.x = 100;
video.y = 100;
video.attachCamera(camera);
addChild(video);
}

[/code]

Note that frames are only captured when the application is in the foreground. If the application moves to the background, capturing is paused but will resume automatically when the application moves to the foreground again.

You can query for the camera properties. Here are a few queries which may be helpful in your development:

[code]

camera.height;
camera.width;
camera.bandwidth;
camera.fps;
camera.muted
camera.name

[/code]

Documentation and Tutorials

Development around video is constantly evolving. The following two resources are among those that will help you to stay informed:

  • The Open Source Media Framework (http://www.opensourcemediaframework .com/resources.html) helps developers with video-related products. It is a good place to find code samples, tutorials, and other materials.
  • Lisa Larson-Kelly specializes in web video publishing and, more recently, mobilepublishing. She offers free tutorials and a newsletter on the latest technology (http://learnfromlisa.com/).

1 COMMENT

  1. Hi, I have tried to use the Camera class on Galaxy S (Android 2.2) but almost all its properties are “-1” (width, height, fps is 0 etc), but Camera.muted == false and also bandwidth returns a number.
    Using AIR 3.3 (tried debug mode, captive-runtime, still the same problem). App is ok on desktop.

    Any idea?

    Thanks!

Comments are closed.