Add Video Capture to Your BlackBerry Applications

Video Capture at Last

Rather than re-invent the wheel, BlackBerry has taken advantage of the existing Multi-Media API (MMAPI) that was created for Java-enabled phones. BlackBerry has supported this API for several years, but only with the release of the 5.0 OS has it expanded its support to include video recording.

MMAPI centers on a class called Player. Many developers have experience creating Players to play back music within their apps, but fewer realize that they can use the same Player class for recording media as well.

Creating a Player for recording is similar to creating one for playback. You might play back audio using something like this:

If you wanted to create a Player suitable for recording, you could instead do this:

Manager.createPlayer("capture://audio");

When you get a Player, you should start it. Unlike playback, which would start playing the music at this point, starting a capture-mode player simply places it into a state where it can begin recording data.

To start the actual recording process, you must obtain a RecordControl. This is a special interface that you can use to manipulate how the recording is supposed to work. It contains options for starting and stopping the recording, and also lets you set a limit on the size of the recorded file. You can obtain a RecordControl in the same way you would retrieve another type of Control for a Player:

Now it's time to do a quick proof of concept that uses a recorder to capture audio. This would be useful if the user wants to speak notes into HomeHunter, and would be a great solution for phones that aren't yet running the 5.0 OS. You should run audio capture in a separate thread; otherwise, the whole user interface will freeze while the player is initializing. This code shows how to start recording when the user makes a selection from the BlackBerry menu:

Finally, when the screen closes, you will clean up the players resources and ensure that all its capture buffers get flushed out. If you skip this step, the user sometimes will be left with a 0-byte file.

Go ahead and run this. On the simulator, configure the memory settings to use the PC filesystem for SD Card files. It will record a silent AMR file. If you try it on a device, you will be able to speak into the microphone and have it capture your words.

Lights, Camera...

Now, on to the video proper. First of all, if you want our app to run on both 5.0 and older versions of the OS, you should query the device to see whether it supports video capture. You can check this with just two lines of code:

Creating the video player is a little more complicated than the audio player. With audio, you could specify a desired audio format, or just use "capture://audio" and have it select an appropriate default. With video, you must say exactly what video format to use. If you already know what formats are available on a particular device, you can specify one directly; this might look something like this:

If you don't care what particular format the device uses, you can grab the first format from the space-delimited videoEncodings value. BlackBerry doesn't support the StringTokenizer class, which would be useful here, but you can pull the first token out manually: