November 23, 2014
Hot Topics:

Add Video Capture to Your BlackBerry Applications

  • December 9, 2009
  • By Chris King
  • Send Email »
  • More Articles »

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:
Manager.createPlayer("file:///SDCard/BlackBerry/music.pcm");

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:

RecordControl recorder = 
    (RecordControl)player.getControl("RecordControl");

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:

private volatile boolean recording;
private Player player;
private RecordControl recorder;
 
private MenuItem startRecording = new MenuItem("Start Recording",
        0, 0)
{
    public void run()
    {
        if (!recording)
        {
            new StartRecordingThread().start();
        }
    }
};
 
private class StartRecordingThread extends Thread
{
    public void run()
    {
        try
        {
            if (player == null || recorder == null)
            {
                player = Manager.createPlayer("capture://audio");
                player.start();
                recorder = (RecordControl) player
                        .getControl("RecordControl");
            }
            recorder.setRecordLocation
    ("file:///SDCard/BlackBerry/temp.amr");
            recorder.startRecord();
            recording = true;
        }
        catch (Throwable t)
        {
            t.printStackTrace();
        }
    }
}

Similarly, the code to stop recording will run in a separate thread and clean up the operation:

private MenuItem stopRecording = new MenuItem("Stop Recording",
        0, 0)
{
    public void run()
    {
        if (recording)
        {
            new StopRecordingThread().start();
        }
    }
};
 
private class StopRecordingThread extends Thread
{
    public void run()
    {
        try
        {
            recorder.stopRecord();
            recorder.commit();
            recording = false;
        }
        catch (Throwable t)
        {
            t.printStackTrace();
        }
    }
}

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.

public boolean onClose()
{
    if (player != null)
    {
        player.close();
    }
    return super.onClose();
}

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:

String videoEncodings = System.getProperty("video.encodings");
boolean hasVideoRecording = videoEncodings != null 
    && videoEncodings.length() > 0; 

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:

capture://video?encoding=video/3gpp&width=480&height=352&video_codec=H263&audio_codec=AMR

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:

int encodingSpace = videoEncodings.indexOf(' ');
if (encodingSpace != -1)
{
    videoEncodings = videoEncodings.substring(0, encodingSpace);
}

If the user tries to use an invalid video format, BlackBerry will not complain initially, but the device will produce an error when the user tries to commit the video at the very end.


Tags: Java, video, Blackberry



Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel