Search code examples
videoblackberry

Recording a video in Blackberry application


I would like to record amd save a Video in my app, there is not a lot of info on this on the forums.

I dont want to invoke the camera like "Invoke.invokeApplication(Invoke.APP_TYPE_CAMERA, new CameraArguments());"

and the following code snippet gives me a blank screen http://docs.blackberry.com/en/developers/deliverables/17968/Record_video_to_a_file_in_a_BB_device_app_1222784_11.jsp

I have come as far to start , stop and save the the video, when I try and play the video it says "Format not supported", is there a way to get all supported formats and is there a list of all video formats? My Code :

public class MyScreen extends MainScreen{
String PATH;
RecordControl _recordControl;
Player _player;
MenuItem RecordVideo;
MenuItem StopVideo;
MenuItem SaveVideo;

    public MyScreen() {
     try {
         _player = javax.microedition.media.Manager.createPlayer("capture://video?encoding=video/3gpp");
         _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 ){
             Dialog.alert("Display size not supported");
         }
         add(videoField);
         _recordControl.setRecordLocation("file:///store/home/user/videos/VideoRecordingTest.3gpp" );   
         _player.start();
         _recordControl.startRecord();    
     }catch( IOException e ){
         Dialog.alert(e.toString());
     } catch( MediaException e ){
         Dialog.alert(e.toString());
     } 
     RecordVideo = new MenuItem("Start Recording", 0, 0){
         public void run() {
             try {
                _player.start();
                 _recordControl.startRecord(); 
            } catch (MediaException e) {
                Dialog.alert("Error Starting recording");
                e.printStackTrace();
            }
        }
    };
    StopVideo = new MenuItem("Stop Recording", 0, 0){
         public void run() {
            try {
                _player.stop();

            } catch (MediaException e) {
                Dialog.alert("Error Stopping recording");
                e.printStackTrace();
            }
        }
    };
    SaveVideo = new MenuItem("Save Video", 0, 0){
         public void run() {
            try {
                // Create an invocation instance with the specified URL where the file type is one of the media types supported by the media player.
                Invocation invocation = new Invocation("file:///SDCard/BlackBerry/music/001.mp3"); 

                // Get the Registry object using the class name of the application      
                Registry _registry=Registry.getRegistry(Application.getApplication().getClass().getName()); 

                //Invoke the content handler.
                _registry.invoke(invocation);
            } catch (IOException e) 
            {      }

        }
     };
 }
 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;
     } 
 }
 protected void makeMenu(Menu menu, int instance) {
        ContextMenu contextMenu = ContextMenu.getInstance();
        contextMenu.setTarget(this);
        contextMenu.clear();
        this.makeContextMenu(contextMenu);
        menu.deleteAll();
        menu.add(contextMenu);
    }

    public void makeContextMenu(ContextMenu contextMenu) {
        contextMenu.addItem(MenuItem.separator(32));
        contextMenu.addItem(RecordVideo);
        contextMenu.addItem(StopVideo);
        contextMenu.addItem(SaveVideo);
    }

}

Solution

  • Full Code For Recoding, Stoping and saving a video, as well as exiting the video:

    Helper:

    public static boolean SdcardAvailabulity() {
     String root = null;
     Enumeration e = FileSystemRegistry.listRoots();
     while (e.hasMoreElements()) {
         root = (String) e.nextElement();
         if( root.equalsIgnoreCase("sdcard/") ) {
             return true;
         }else if( root.equalsIgnoreCase("store/") ) {
             return false;
         }
     }
     class MySDListener implements FileSystemListener {
         public void rootChanged(int state, String rootName) {
             if( state == ROOT_ADDED ) {
                 if( rootName.equalsIgnoreCase("sdcard/") ) {
                 }
             } else if( state == ROOT_REMOVED ) {
             }
         }
     }
     return true;
    }
    

    Main Class

    String _encoding = "encoding=video/3gpp&mode=standard";
    private static Player _player;
    private static VideoControl _vc;
    private RecordControl _rc;
    private static boolean _locationSet = false;
    private static OutputStream _out;
    private static FileConnection _fc;
    String  PATH;
    String STREAM_VIDEO_FILE;
    String StringVideofileName;
    /*LabelField GetVideofileName = new LabelField("",LabelField.FOCUSABLE){
        protected boolean navigationClick(int status, int time){
            Dialog.alert("Clicked");
            return true;
        }
    };*/
    protected boolean keyChar( char c, int status, int time ){
        switch( c ) {
            case 's':
                startRecord();
                return true;
            case 'x':
                stopRecord();
                return true;
            case 'c':
                commit();
                return true;
            default:
                return false;
        }
    }
    private void startRecord(){
        try {
            if( !_locationSet ){     
                try {
                    System.out.println("STREAM_VIDEO_FILE-------------"+STREAM_VIDEO_FILE);
                    _fc = (FileConnection)Connector.open( STREAM_VIDEO_FILE ); 
    
                    if( !_fc.exists() ){
                        _fc.create();
                    }                            
                    _fc.truncate( 0 );                         
                    _out = _fc.openOutputStream();
                } catch ( Exception e ) {
                    return;
                }
                _rc.setRecordStream( _out );
                _locationSet = true;
            }               
            _rc.startRecord();
        } catch ( Exception e ) {
        }
    }
    private void stopRecord(){
        try {             
            _rc.stopRecord();
        } catch ( Exception e ) {
        }
    }
    private void commit() {
        try {           
            _rc.commit();
            Dialog.alert( "Saved" );
            _locationSet = false;
            try {
                _out.close();
                _fc.close();
                StringVideofileName =_fc.getName();
                DefaultScreen.LF.setText(StringVideofileName);
                 if(_player!=null)
                     _player.close();          
            }catch(Exception e){
                if(_player!=null){
                    _player.close();
                }
                if(_fc!=null){
                    try{
                        _fc.close();
                    }catch (IOException e1){ 
                    }
                }                    
            }
        } catch ( Exception e ) {
        }
    }
    public MyScreen(){
        if(Sh.SdcardAvailabulity()){
            PATH = System.getProperty("fileconn.dir.memorycard.photos")+"Video_"+System.currentTimeMillis()+".mp4";//here "str" having the current Date and Time;
        } else {
            PATH = System.getProperty("fileconn.dir.photos")+"Video_"+System.currentTimeMillis()+".mp4"; 
        }
        STREAM_VIDEO_FILE = PATH;
        try {           
            _player = javax.microedition.media.Manager.createPlayer( "capture://video?" + _encoding );               
            _player.start();
            _vc = (VideoControl)_player.getControl( "VideoControl" );
            _rc = (RecordControl)_player.getControl( "RecordControl" );              
            final Field videoField = (Field)_vc.initDisplayMode( VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field" );              
            _vc.setDisplaySize( Display.getWidth(), Display.getHeight() );
            add( videoField );
        } catch ( final Exception e ) {
            System.out.println( "Exception in VideoScreen constructor" );
        }
        addMenuItem( new MenuItem( "Start Video Recording", 0, 0 ){
            public void run(){
                startRecord();
            }
        });
        addMenuItem( new MenuItem( "Stop Video Recdording", 0, 0 ){
            public void run(){
                stopRecord();
            }
        });
        addMenuItem( new MenuItem( "Save Video Recording", 0, 0 ){
            public void run(){
                commit();
                UiApplication.getUiApplication().invokeLater(new Runnable() {                            
                        public void run() {
                        UiApplication.getUiApplication().popScreen(MyScreen.this);
                        }
                    }); 
            }
        });
    }
    }