September 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Add Video Capture to Your BlackBerry Applications

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

Getting a View

When you have a video player, you should add a viewfinder to your app. This will let users see just what they're recording--an important feature! You gain access to video-specific capabilities by retrieving a VideoControl object, in the same way you got a RecordControl.

Now, you need to put something on the screen. You can retrieve an appropriate component by invoking the initDisplayMode() method. Because you are building a BlackBerry CLDC UI application, you will request a Field for your screen:

Field cameraView = (Field)control.initDisplayMode
    (VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");

Author's Note: If you're creating a BlackBerry MIDlet application, you could instead have asked for a "javax.microedition.lcdui.Item" display component. You could then insert this component into a MIDlet's Form.

When and where should you create the viewfinder? Ordinarily, you would create all of your Field objects within the constructor. That's fine to do here, but it can take a while for the device to initialize the camera, which will cause a noticeable pause at that stage. Instead, you will wait until the user starts recording. If a Field hasn't already been created, you can do it then.

Be sure not to skip the viewfinder! Even after you create the viewfinder, if it isn't visible on the screen, the video will not record properly.

Once the viewfinder is in place, you can see the camera's current perspective (see Figure 2).


Figure 2. HomeHunter Actively Recording:
Insert a viewfinder into the screen to hook into the camera.

Wrapping Up

Believe it or not, you're almost done. The final recording method looks like this:

String videoEncodings = System.getProperty("video.encodings");
boolean hasVideoRecording = videoEncodings != null && videoEncodings.length() > 0;
if (player == null || recorder == null)
{
    if (hasVideoRecording)
    {
        int encodingSpace = videoEncodings.indexOf(' ');
        if (encodingSpace != -1)
        {
            videoEncodings = videoEncodings
                    .substring(0, encodingSpace);
        }
        player = Manager.createPlayer("capture://video?"
                        + videoEncodings);
        player.start();
        VideoControl video = (VideoControl) player
                .getControl("VideoControl");
        final Field cameraView = (Field) video.initDisplayMode(
                        VideoControl.USE_GUI_PRIMITIVE,
                        "net.rim.device.api.ui.Field");
        getApplication().invokeLater(new Runnable()
        {
            public void run()
            {
                add(cameraView);
            }
        });
    }
    else
    {
        player = Manager.createPlayer("capture://audio");
        player.start();
    }
    recorder = (RecordControl) player.getControl("RecordControl");
}
recorder.setRecordLocation("file:///store/home/user/hunted.3gp");
recorder.startRecord();
recording = true; 

See Listing 1 to view the entire Screen.

When you run this on your computer, the simulator will check to see if you have a supported webcam installed. If so, you will be able to test the video recording function with your webcam. Otherwise, you will be prompted to select a still image file like a JPG or PNG; this is still useful for testing, if less interesting. On the actual device, you can see the video being captured by the camera.

To play back your captured video, you can browse the phone filesystem. The easiest way to do this is to visit the Media application, press the BlackBerry menu key, and then select Explore.

Author's Note: Be sure to test on your target devices before shipping your application. Video support varies from device to device and from version to version. For example, recording to a file works well on version 5.0.0.252, but the same code may freeze on 5.0.0.328. Considering Research In Motion's track record, video recording should become more stable and reliable in future versions.

In addition to saving the captured video to a file, you can also direct the video to an OutputStream. This is very useful if you intend to process the video in memory or upload it to a network. Unfortunately, early versions of the 5.0 OS contain a bug that throws a NullPointerException when you attempt to commit a video to a stream. If this problem occurs for you, you can save to a file and then open the file for further processing.

Just like that, you've taken a useful app and made it more useful, more flashy, and more compelling. Because video uses existing APIs, you can even create a single application that displays video on recent BlackBerry devices and also runs on older devices without video. Video makes a statement, and it will show even skeptics that BlackBerry devices stand up to today's hottest phones.

About the Author

Chris King is a senior software engineer at Gravity Mobile and is the author of Advanced BlackBerry Development (published by Apress). When he isn't programming or writing for fun or profit, Chris can be found reading, baking, cycling, or hiking throughout the San Francisco Bay Area.


Tags: Java, video, Blackberry



Page 3 of 3



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel