Record video to a file in a BlackBerry device application

  1. Import the required classes.
    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.component.*;
    import net.rim.device.api.ui.container.*;
    import net.rim.device.api.system.*;
    import java.io.*;
    import java.lang.*;
    import javax.microedition.media.*;
    import javax.microedition.media.control.*;
  2. Create the application framework by extending the UiApplication class. In main(), create an instance of the new class and invoke enterEventDispatcher() to enable the application to receive events. In the application constructor, invoke pushScreen() to display the custom screen for the application. The VideoRecordingDemoScreen class, which is described in step 3, represents the custom screen.
    public class VideoRecordingDemo extends UiApplication
    {
         public static void main(String[] args)
         {
              VideoRecordingDemo app = new VideoRecordingDemo();
              app.enterEventDispatcher();
         }
        
         public VideoPlaybackDemo()
         {
              pushScreen(new VideoRecordingDemoScreen());
         }
    }
  3. Create the framework for the custom screen by extending the MainScreen class. Declare an instance of the VideoRecorderThread class, which is described in step 5.
    class VideoRecordingDemoScreen extends MainScreen
    {	
        private VideoRecorderThread _recorderThread;
    
        public VideoRecordingDemoScreen()
        {
        }
    }
  4. In the VideoRecordingDemoScreen constructor, invoke setTitle() to specify the title for the screen. Invoke addMenuItem() twice to add the menu items to start and stop the recording, which is described in step 5.
    public VideoRecordingDemoScreen()
    {
       setTitle("Video recording demo");
    
       addMenuItem(new StartRecording());
       addMenuItem(new StopRecording());
                
    }
  5. In the VideoRecordingDemoScreen class, define the menu items to start recording and to stop recording by invoking the start() and stop() methods of the VideoRecorderThread class respectively, which is described in step 6.
    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());
             }
         }
    }
  6. Inside 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.
    Note: You are not required 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()
        {
        
        }
    
    }
  7. 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. Only the encoding video/3gpp is currently supported.
    public void run() 
    {
        try 
        {
            _player = javax.microedition.media.Manager.createPlayer("capture://video?encoding=video/3gpp");
        
        }
        catch( IOException e ) 
        {
            Dialog.alert(e.toString());
        }
        catch( MediaException e ) 
        {
            Dialog.alert(e.toString());
        }
    }
  8. Invoke Player.addPlayerListener() specifying this as a parameter because VideoRecorderThread implements the PlayerListener interface.
    public void run() 
    {
        try 
        {
            _player = javax.microedition.media.Manager.createPlayer("capture://video?encoding=video/3gpp");
    
            _player.addPlayerListener(this);
        
        }
        catch( IOException e ) 
        {
            Dialog.alert(e.toString());
        }
        catch( MediaException e ) 
        {
            Dialog.alert(e.toString());
        }
    }
  9. Invoke Player.realize() to access the Control object. Then , invoke Player.getControl() passing in the string, VideoControl, to retrieve the VideoControl object that is associated with Player.
    public void run() 
    {
        try 
        {
            _player = javax.microedition.media.Manager.createPlayer("capture://video?encoding=video/3gpp");
    
            _player.addPlayerListener(this);
    
            _player.realize();
            VideoControl videoControl = (VideoControl) _player.getControl("VideoControl");
        
        }
        catch( IOException e ) 
        {
            Dialog.alert(e.toString());
        }
        catch( MediaException e ) 
        {
            Dialog.alert(e.toString());
        }
    }
  10. Invoke Player.getControl(), passing in the string, RecordControl, to retrieve the RecordControl object.
    public void run() 
    {
        try 
        {
            _player = javax.microedition.media.Manager.createPlayer("capture://video?encoding=video/3gpp");
    
            _player.addPlayerListener(this);
    
            _player.realize();
            VideoControl videoControl = (VideoControl) _player.getControl("VideoControl");
            _recordControl = (RecordControl) _player.getControl( "RecordControl" );
        
        }
        catch( IOException e ) 
        {
            Dialog.alert(e.toString());
        }
        catch( MediaException e ) 
        {
            Dialog.alert(e.toString());
        }
    }
  11. Invoke VideoControl.initDisplayMode(int mode, Object arg) passing an arg parameter specifying the UI primitive that displays the video to initialize the mode that a video field uses. Cast the returned object as a Field object.
    Note: You can invoke initDisplayMode() in different ways to return a Field, an Item for use with MIDlets, or to display a video on a Canvas class.
    public void run() 
    {
        try 
        {
            _player = javax.microedition.media.Manager.createPlayer("capture://video?encoding=video/3gpp");
    
            _player.addPlayerListener(this);
    
            _player.realize();
            VideoControl videoControl = (VideoControl) _player.getControl("VideoControl");
            _recordControl = (RecordControl) _player.getControl( "RecordControl" );
    
            Field videoField = (Field) videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
        
        }
        catch( IOException e ) 
        {
            Dialog.alert(e.toString());
        }
        catch( MediaException e ) 
        {
            Dialog.alert(e.toString());
        }
    }
  12. In a try/catch block, invoke VideoControl.setDisplaySize() to set the size of the viewfinder to monitor your recording. In the following code sample, the size is set to the full screen of the device. Invoke add() to add the viewfinder to the screen.
    public void run() 
    {
        try 
        {
            _player = javax.microedition.media.Manager.createPlayer("capture://video?encoding=video/3gpp");
    
            _player.addPlayerListener(this);
    
            _player.realize();
            VideoControl videoControl = (VideoControl) _player.getControl("VideoControl");
            _recordControl = (RecordControl) _player.getControl( "RecordControl" );
    
            Field videoField = (Field) videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
    
            try
            {
                videoControl.setDisplaySize( Display.getWidth(), Display.getHeight() );
            }
            catch( MediaException me )
            {
                // setDisplaySize is not supported
            }
            
            add(videoField);
        }
        catch( IOException e ) 
        {
            Dialog.alert(e.toString());
        }
        catch( MediaException e ) 
        {
            Dialog.alert(e.toString());
        }
    }
  13. Invoke RecordControl.setRecordLocation() to specify the location on the device to save the video recording.
    public void run() 
    {
        try 
        {
            _player = javax.microedition.media.Manager.createPlayer("capture://video?encoding=video/3gpp");
    
            _player.addPlayerListener(this);
    
            _player.realize();
            VideoControl videoControl = (VideoControl) _player.getControl("VideoControl");
            _recordControl = (RecordControl) _player.getControl( "RecordControl" ); 
    
            Field videoField = (Field) videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
    
            try
            {
                videoControl.setDisplaySize( Display.getWidth(), Display.getHeight() );
            }
            catch( MediaException me )
            {
                // setDisplaySize is not supported
            }
    
            add(videoField);
      
            _recordControl.setRecordLocation("file:///store/home/user/VideoRecordingTest.3gpp" );      
        }
        catch( IOException e ) 
        {
            Dialog.alert(e.toString());
        }
        catch( MediaException e ) 
        {
            Dialog.alert(e.toString());
        }
    }
  14. Invoke RecordControl.startRecord() to start recording the video and start playing the media from Player. Invoke Player.start() to start Player.
    public void run() 
    {
        try 
        {
            _player = javax.microedition.media.Manager.createPlayer("capture://video?encoding=video/3gpp");
    
            _player.addPlayerListener(this);
    
            _player.realize();
            VideoControl videoControl = (VideoControl) _player.getControl("VideoControl");
            _recordControl = (RecordControl) _player.getControl( "RecordControl" );   
    
            Field videoField = (Field) videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
    
            try
            {
                videoControl.setDisplaySize( Display.getWidth(), Display.getHeight() );
            }
            catch( MediaException me )
            {
                // setDisplaySize is not supported
            }
            
            add(videoField);        
    
            _recordControl.setRecordLocation("file:///store/home/user/VideoRecordingTest.3gpp" );
    
            _recordControl.startRecord(); 
            _player.start();
        
        }
        catch( IOException e ) 
        {
            Dialog.alert(e.toString());
        }
        catch( MediaException e ) 
        {
            Dialog.alert(e.toString());
        }
    }
  15. 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. Set the Player to null.
    public void stop() 
    {
        if (_player != null) 
        {
            _player.close();
            _player = null;
        }
    }
  16. 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. Set RecordControl to null.
    public void stop() 
    {
        if (_player != null) 
        {
            _player.close();
            _player = null;
        }
    
        if (_recordControl != null) 
        {
            _recordControl.stopRecord();
                        
            try 
            {
                _recordControl.commit();
            } 
            catch (Exception e) 
            {
                Dialog.alert(e.toString());
            }
            _recordControl = null;
        }
    }
  17. In VideoRecorderThread, implement the PlayerListener interface's playerUpdate() method which is invoked when the Player object generates an event. In the following code sample, information about the event is displayed.
    public void playerUpdate(Player player, String event, Object eventData) 
    {
        Dialog.alert("Player " + player.hashCode() + " got event " + event + ": " + eventData);
    }
Previous topic: Recording video

Was this information helpful? Send us your comments.