Would you like to tell us how we are doing?

You bet No thanks

Sorry about the red box, but we really need you to update your browser. Read this excellent article if you're wondering why we are no longer supporting this browser version. Go to Browse Happy for browser suggestions and how to update.

Working with video

The BlackBerry Java SDK lets you to record and play back video in your app.

You can record video using the javax.microedition.media.Player interface and the RecordControl interface. The recording is saved to a file in built-in media storage, media card storage, or to a stream. When you create a Player object, you can specify various properties of the recording, such as the bitrate, the audio codec, and the input scene mode. The VideoControl.initDisplayMode() method allows you to create a viewfinder that allows your user to monitor the recording. The initDisplayMode() method returns a Field object that you can add to your UI.

You can also use the Player interface to play video in your app. Use the Player class to open a file that is stored in the built-in media storage, a media card, or in your. cod file. To create a UI component that displays the video playback, you can invoke VideoControl.initDisplayMode() to retrieve a Field object that is added to your UI.

From your Player object, you can retrieve the associated Control interfaces that enable you to control various aspects of playback. For example, you can control the volume of the audio, or control the video buffer.

Whether you are recording or playing video, you can overlay UI controls on the video display with the AdvancedVideoControl interface.

For more information about controlling the encoding and other camera properties, see Media recording properties.

For more information about the media formats supported on BlackBerry devices, download the following PDF: http://docs.blackberry.com/en/smartphone_users/deliverables/18349/711-01774-123_Supported_Media_Types_on_BlackBerry_Smartphones.pdf

Rendering graphics over video fields

You can use the net.rim.device.api.media.control.AdvancedVideoControl interface to define a video display mode that allows you to render core UI components over video fields. AdvancedVideoControl extends javax.microedition.media.control.VideoControl to provide additional functionality. For example, you can place playback controls over a video that is playing in your application. The AdvancedVideoControl interface provides a mode constant, USE_GUI_ADVANCED, that you pass to VideoControl.initDisplayMode().

When you pass USE_GUI_ADVANCED to initDisplayMode(), the object argument that you pass with it must be the java.lang.String "net.rim.device.api.ui.Field". When you invoke initDisplayMode(), it returns an instance of net.rim.device.api.ui.Field.

You can specify the region where you want to render video by invoking VideoControl.setDisplayLocation(). The default location is (0,0). The USE_GUI_ADVANCED mode does not support drawing other graphics at the same region on the canvas directly; however, you can overlay other Fields or Managers while this mode is active.

To obtain an AdvancedVideocontrol object, invoke Player.getControl(), and pass in the fully qualified name of the control.

Field field;

try 
{
    Player p = Manager.createPlayer("<URL of video source>");
    p.realize();

    AdvancedVideoControl vc;
    if ((vc = (AdvancedVideoControl) p.getControl(
         "net.rim.device.api.media.control.AdvancedVideoControl")) != null)
    {
        field = (Field)vc.initDisplayMode(AdvancedVideoControl.USE_GUI_ADVANCED,
                 "net.rim.device.api.ui.Field");
        vc.setVisible(true);
    }
    p.start();
} 
catch (MediaException pe)
{
    // Code that runs if a player cannot be created.
}
catch (IOException ioe) 
{
    // Code that runs if the video source is not accessible.
}

Code sample: Playing video in a UI field in a BlackBerry device application

Code sample: Recording video to a file in a BlackBerry device application

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.system.*;
import java.lang.*;
import javax.microedition.media.*;
import java.io.*;
import javax.microedition.media.control.*;

//Create the application framework by extending the UiApplication class. 
//In main(), create an instance of the new class and invoke 
//enterEventDispatcher() to enable the app to receive events. 
//In the application constructor, invoke pushScreen() to display the custom 
//screen for the app. The VideoRecordingDemoScreen class represents 
//the custom screen.
public class VideoRecordingDemo extends UiApplication
{
  public static void main(String[] args)
  {
       VideoRecordingDemo app = new VideoRecordingDemo();
       app.enterEventDispatcher();
  }
    
  public VideoRecordingDemo()
  {
       pushScreen(new VideoRecordingDemoScreen());
  }

//Create the framework for the custom screen by extending the MainScreen 
//class. Declare an instance of the VideoRecorderThread class.  
  private class VideoRecordingDemoScreen extends MainScreen  
  {	
      private VideoRecorderThread _recorderThread;

//In the VideoRecordingDemoScreen constructor, invoke setTitle() to specify the 
//title for the screen. Invoke addMenuItem() twice to add the menu items 
//that start and stop the recording.  
      public VideoRecordingDemoScreen()
      {
          setTitle("Video recording demo");

          addMenuItem(new StartRecording());
          addMenuItem(new StopRecording());
      }

//In the VideoRecordingDemoScreen class, define the menu items to start and stop 
//the recording. The menu items invoke the VideoRecorederThread.start() and 
//VideoRecorederThread.stop() methods.  
      private class StartRecording extends MenuItem 
      {
          public StartRecording() 
          {
              super("Start recording", 0, 100);
          }

          public void run() 
          {
              try 
              {
                  VideoRecorderThread newRecorderThread = new VideoRecorderThread();
                  newRecorderThread.start();
                  _recorderThread = newRecorderThread;
              }  
              catch (Exception e) 
              {
                  Dialog.alert(e.toString());
              }
          }
      }
        
      private class StopRecording extends MenuItem 
      {
          public StopRecording() 
          {
              super("Stop recording", 0, 100);
          }

          public void run() 
          {
              try 
              {
                  if (_recorderThread != null) 
                  { 
                      _recorderThread.stop();
                  }
              } 
              catch (Exception e) 
              {
                  Dialog.alert(e.toString());
              }
          }
      }

//In the VideoRecordingDemo screen class, define an inner class that extends 
//Thread and implements PlayerListener. In the class, create a variable of 
//type Player, and a variable of type RecordControl for recording media from 
//Player. You do not need to record video in a separate thread because 
//recording operations are threaded by design.
      private class VideoRecorderThread extends Thread implements 
            javax.microedition.media.PlayerListener
      {
          private Player _player;
          private RecordControl _recordControl;

          VideoRecorderThread()
          {
          }

//In the VideoRecorderThread class, implement run(). In run() create a try/catch 
//block and invoke Manager.createPlayer(String locator) to create a Player object 
//to capture video, using as a parameter a value that specifies the encoding 
//to use to record video.
          public void run() 
          {
              try 
              {
                  _player = javax.microedition.media.Manager.createPlayer(
                              "capture://video?encoding=video/3gpp");
//Invoke Player.addPlayerListener(). Specify this as a parameter because 
//VideoRecorderThread implements PlayerListener.
                  _player.addPlayerListener(this);

//Invoke Player.realize() to initialize the VideoControl object.
                  _player.realize();

//Invoke Player.getControl("VideoControl") to retrieve the VideoControl object.  
                  VideoControl videoControl = 
                                   (VideoControl) _player.getControl("VideoControl");
                  _recordControl = (RecordControl) _player.getControl( "RecordControl" ); 

//Invoke VideoControl.initDisplayMode(int mode, Object arg). To initialize the mode 
//that a video field uses, pass an arg parameter to specify the UI primitive that 
//displays the video. Cast the returned object as a Field object. You can invoke 
//initDisplayMode() in different ways to return a Field (or an Item to display a 
//video on a Canvas class in a MIDlet).  
                  Field videoField = 
                      (Field) videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE,
                      "net.rim.device.api.ui.Field");

                  try
                  {

//In a try/catch block, invoke VideoControl.setDisplaySize() to set the size of the 
//viewfinder to monitor your recording. In this example, the size is the full screen.
//Invoke add() to add the viewfinder to the screen.  
                      videoControl.setDisplaySize(Display.getWidth(), Display.getHeight() );
                  }
                  catch( MediaException me )
                  {
                      // setDisplaySize is not supported
                  }
                    
                  add(videoField);

//Invoke RecordControl.setRecordLocation() to specify the location on the device 
//to save the video recording.  
                  _recordControl.setRecordLocation(
                       "file:///store/home/user/VideoRecordingTest.3gpp" );   

//Invoke RecordControl.startRecord() to start recording the video and start 
//playing the media from Player. Invoke Player.start() to start Player.  
                  _recordControl.startRecord(); 
                  _player.start();
    
              }
              catch( IOException e ) 
              {
                  Dialog.alert(e.toString());
              }
              catch( MediaException e ) 
              {
                  Dialog.alert(e.toString());
              }
          }

//In VideoRecorderThread, implement the Thread interface's stop method. Check that 
//Player is not null and invoke Player.close() to release the Player object's 
//resources. Then set the Player to null.  
          public void stop() 
          {
              if (_player != null) 
              {
                   _player.close();
                   _player = null;
              }

//Check that RecordControl is not null and invoke RecordControl.stopRecord() to 
//stop recording. In a try/catch block, invoke RecordControl.commit() to save 
//the recording to a specified file. Then set RecordControl to null.  
              if (_recordControl != null) 
              {
                  _recordControl.stopRecord();
                    
                  try 
                  {
                      _recordControl.commit();
                  } 
                  catch (Exception e) 
                  {
                      Dialog.alert(e.toString());
                  }
                  _recordControl = null;
              } 
          }

//In VideoRecorderThread, implement the PlayerListener interface's 
//playerUpdate() method, which is invoked when the Player object generates an 
//event. In this example, information about the event is displayed.  
          public void playerUpdate(Player player, String event, Object eventData) 
          {
              Dialog.alert("Player " + player.hashCode() + " got event " + event + ": " 
                              + eventData);
          }
      }
  }
}