在Blackberry应用程序中录制视频

时间:2012-11-05 10:35:00

标签: video blackberry

我想在我的应用中记录amd保存视频,论坛上没有很多关于此的信息。

我不想像“Invoke.invokeApplication(Invoke.APP_TYPE_CAMERA,new CameraArguments())”那样调用相机;“

以下代码段给我一个空白屏幕http://docs.blackberry.com/en/developers/deliverables/17968/Record_video_to_a_file_in_a_BB_device_app_1222784_11.jsp

我已经开始,停止并保存视频,当我尝试播放视频时显示“格式不支持”,有没有办法获得所有支持的格式,并且是否有所有视频的列表格式? 我的代码:

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);
    }

}

1 个答案:

答案 0 :(得分:0)

完整代码用于录制,停止和保存视频以及退出视频:

助手:

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;
}

主类

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);
                    }
                }); 
        }
    });
}
}