2012-11-05 73 views
-1

我想在我的應用程序中錄製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

@Eugen馬丁諾夫,我們對此深感抱歉,會記住下一次,並嘗試張貼簡潔的代碼,請不要裸陪我,如果我真的犯了錯誤,STIL新發展。 @Miguel對不起,Didntknow如何接受anwers爲sollutions,感謝您的鏈接

完整代碼重新編碼,回採和保存的視頻,以及退出視頻:

助手:

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