我正在嘗試開發一個應用程序,可以從其用戶錄製視頻。所以我需要用前置攝像頭拍攝視頻(如果有的話)。 我建立了一個相機預覽,這工作正常。 我使用Android How-To Sites來構建MediaRecorder並進行設置。 如果我使用CamcorderProfile,則在調用start()時我的媒體服務器會死亡。 如果我自己設置編碼器,媒體服務器會在start()中引發一個運行時異常,並顯示消息「start failed:-19」 我在這裏發現了一些關於此主題的問題,但沒有解決我的問題。 我認爲這可能與我沒有使用背面照相機有關。也許我沒有找到正確的紀錄片來建立正確的代碼。我認爲這不僅是我的問題,而且我很樂意獲得關於相機使用情況的更多知識。 我的代碼如下:MediaRecorder在Android上啓動視頻捕獲問題
其中所述預覽設置
protected void onResume() {
super.onResume();
// 1. set up camera preview
if(checkCameraHardware(this)){
mCamera = getCameraInstance();
mCameraPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(id.cameraPreview);
preview.addView(mCameraPreview);
}
else{
Log.d("Recorder", "camera check returned false");
}
}
所用方法checkCameraHardware()
private boolean checkCameraHardware(Context context){
boolean ret = true;
if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
ret = true;
}
else {
ret = false;
}
return ret;
}
和方法getCameraInstance()
public static Camera getCameraInstance(){
Camera c = null;
int cnum = 0;
mCamSelect = 0;
Camera.CameraInfo caminfo = new CameraInfo();
try {
cnum = Camera.getNumberOfCameras();
Log.d("getCameraInstance", String.valueOf(cnum));
for(int i = 0;i<cnum;i++){
Camera.getCameraInfo(i, caminfo);
if(caminfo.facing == CameraInfo.CAMERA_FACING_FRONT){
mCamSelect = i;
break;
}
}
c = Camera.open(mCamSelect); // attempt to get a Camera instance
}
catch (Exception e){
Log.d("getCameraInstance", "FATAL camera could not be opened");
// Camera is not available (in use or does not exist)
}
if(c==null)Log.d("getCameraInstance", "no camera returned");
return c; // returns null if camera is unavailable
}
所述的onResume()
此代碼片斷顯示錯誤出現的位置(在onClick回調中)
if(prepareVideoRecorder()){
mMediaRecorder.start(); //here the errors occure
recording = true;
//start recording
}
和三個MediaRecorder相關的方法:prepareVideoRecorder(),releaseMediaRecorder()和釋放相機()
private void releaseMediaRecorder(){
if (mMediaRecorder != null) {
mMediaRecorder.reset(); // clear recorder configuration
mMediaRecorder.release(); // release the recorder object
mMediaRecorder = null;
mCamera.lock(); // lock camera for later use
}
}
private void releaseCamera(){
if (mCamera != null){
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
private boolean prepareVideoRecorder(){
//ex: mCamera = getCameraInstance();
mMediaRecorder = new MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
// Step 2: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
CamcorderProfile profile = CamcorderProfile.get(mCamSelect, CamcorderProfile.QUALITY_HIGH);
if(profile == null){Log.d(tag, "the camcorder profile instance is null");
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
}else{
mMediaRecorder.setProfile(profile);
}
// Step 4: Set output file
//ex: mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
mMediaRecorder.setOutputFile(currentVidFile.getAbsolutePath());
// Step 5: Set the preview output
mMediaRecorder.setPreviewDisplay(mSlideview.getHolder().getSurface());
// Step 6: Prepare configured MediaRecorder
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
Log.d(tag, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
} catch (IOException e) {
Log.d(tag, "IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}
return true;
}
public void onGesturePerformed(GestureOverlayView arg0, Gesture arg1) {
// TODO Auto-generated method stub
}
}
您正在測試什麼硬件和Android版本?某些Samsung和HTC設備需要設置「cam-mode = 1」參數。 – Fildor
我只是在Dalvic虛擬機上進行測試。我在我的虛擬機上使用Android API版本16。我通過VM Manager將相機的屬性添加到VM。使用的凸輪可用於預覽。 – Semo
對於我所聽到和了解到的所有內容,在模擬器上使用MediaRecorder將不起作用。如果有人知道我說的是錯的,請告訴我!因爲在這種情況下,我會感興趣如何去做;) – Fildor