2

我正在爲我的應用程序創建錄像機功能。我用下面的代碼準備相機和媒體記錄API:選項卡上的視頻錄製(Nexus 7)失敗

private boolean prepareCameraRecorder() { 
    // BEGIN_INCLUDE (configure_preview) 
    int numCameras = Camera.getNumberOfCameras(); 
    int camId = 0; 

    if (numCameras == 1) { 
     PackageManager pm = getPackageManager(); 

     boolean frontCam = pm 
       .hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT); 
     boolean rearCam = pm 
       .hasSystemFeature(PackageManager.FEATURE_CAMERA); 

     if (frontCam) { 
      camera = CameraHelper.getDefaultFrontFacingCameraInstance(); 
      camId = Camera.CameraInfo.CAMERA_FACING_BACK; 
     } else if (rearCam) { 
      camera = CameraHelper.getDefaultBackFacingCameraInstance(); 
      camId = Camera.CameraInfo.CAMERA_FACING_FRONT; 
     } 

    } else if (numCameras == 2) { 
     if (CAMERA_TYPE == CAMERA_FRONT) { 
      Utils.getInstance().printDebug("displaying front camera "); 
      camera = CameraHelper.getDefaultFrontFacingCameraInstance(); 
      camId = Camera.CameraInfo.CAMERA_FACING_FRONT; 
     } else if (CAMERA_TYPE == CAMERA_BACK) { 
      Utils.getInstance().printDebug("displaying back camera "); 
      camera = CameraHelper.getDefaultBackFacingCameraInstance(); 
      camId = Camera.CameraInfo.CAMERA_FACING_BACK; 
     } 
    } 

    // We need to make sure that our preview and recording video size are 
    // supported by the 
    // camera. Query camera to find all the sizes and choose the optimal 
    // size given the 
    // dimensions of our preview surface. 
    parameters = camera.getParameters(); 
    List<Camera.Size> mSupportedPreviewSizes = parameters 
      .getSupportedPreviewSizes(); 
    Camera.Size optimalSize = CameraHelper.getOptimalPreviewSize(
      mSupportedPreviewSizes, cameraView.getWidth(), 
      cameraView.getHeight()); 

    // Use the same size for recording profile. 

    // Check profile in tab: 

    int profile = -1; 

    // set camera profile 
    if (CamcorderProfile.hasProfile(camId, CamcorderProfile.QUALITY_HIGH)) { 
     profile = CamcorderProfile.QUALITY_HIGH; 
    } else if (CamcorderProfile.hasProfile(camId, 
      CamcorderProfile.QUALITY_480P)) { 
     profile = CamcorderProfile.QUALITY_480P; 
    } else if (CamcorderProfile.hasProfile(camId, 
      CamcorderProfile.QUALITY_720P)) { 
     profile = CamcorderProfile.QUALITY_720P; 
    } else if (CamcorderProfile.hasProfile(camId, 
      CamcorderProfile.QUALITY_1080P)) { 
     profile = CamcorderProfile.QUALITY_1080P; 
    } else if (CamcorderProfile.hasProfile(camId, 
      CamcorderProfile.QUALITY_CIF)) { 
     profile = CamcorderProfile.QUALITY_CIF; 
    } else if (CamcorderProfile.hasProfile(camId, 
      CamcorderProfile.QUALITY_LOW)) { 
     profile = CamcorderProfile.QUALITY_LOW; 
    } else if (CamcorderProfile.hasProfile(camId, 
      CamcorderProfile.QUALITY_QCIF)) { 
     profile = CamcorderProfile.QUALITY_QCIF; 
    } else if (CamcorderProfile.hasProfile(camId, 
      CamcorderProfile.QUALITY_QVGA)) { 
     profile = CamcorderProfile.QUALITY_QVGA; 
    } 
    // 

    CamcorderProfile camProfile = null; 
    camProfile = CamcorderProfile.get(camId, profile); 

    if (profile != -1) { 
     Utils.getInstance().printDebug("profile: " + profile); 
     camProfile.videoFrameWidth = optimalSize.width; 
     camProfile.videoFrameHeight = optimalSize.height; 

     parameters.setPreviewSize(optimalSize.width, optimalSize.height); 
     camera.setParameters(parameters); 
     parameters = camera.getParameters(); 
    } 
    // else { 
    // parameters.setPreviewSize(100, 100); 
    // camera.setParameters(parameters); 
    // } 
    try { 
     camera.stopPreview(); 
     camera.setPreviewCallback(null); 
    } catch (Exception e) { 
     // ignore: tried to stop a non-existent preview 
    } 

    // set preview size and make any resize, rotate or 
    // reformatting changes here 

    // start preview with new settings 
    try { 
     camera.setPreviewDisplay(holder); 
     camera.startPreview(); 
     camera.setDisplayOrientation(90); 
    } catch (Exception e) { 
     Utils.getInstance().printDebug(
       "Error starting camera preview: " + e.getMessage()); 
    } 
    // BEGIN_INCLUDE (configure_media_recorder) 
    mediaRecorder = new MediaRecorder(); 
    mediaRecorder.reset(); 

    // Step 1: Unlock and set camera to MediaRecorder 
    camera.unlock(); 
    mediaRecorder.setCamera(camera); 

    // Step 2: Set sources 
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); 
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 

    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher) 
    if (profile != -1) { 
     mediaRecorder.setProfile(camProfile); 
    } 

    try { 

     // Step 4: Set output file 
     selFilePath = CameraHelper.getOutputMediaFile(
       CameraHelper.MEDIA_TYPE_VIDEO).toString(); 
     mediaRecorder.setOutputFile(selFilePath); 
     Utils.getInstance().printDebug("selFilePath: " + selFilePath); 

     // Step 5: Prepare configured MediaRecorder 
     // if (profile == -1) { 
     // mediaRecorder.setVideoFrameRate(30); 
     // } 

     mediaRecorder.setOrientationHint(90); 
     //mediaRecorder.setVideoSize(optimalSize.width, optimalSize.height); 
     //mediaRecorder.setVideoFrameRate(10); 

     // mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
     // mediaRecorder.setAudioEncodingBitRate(192000); 
     // mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); 
     // mediaRecorder.setVideoEncodingBitRate(12000000); 
     // mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); 

     mediaRecorder.prepare(); 
    } catch (IllegalStateException e) { 
     Log.d(TAG, 
       "IllegalStateException preparing MediaRecorder: " 
         + e.getMessage()); 
     e.printStackTrace(); 
     releaseMediaRecorder(); 
     return false; 
    } catch (Exception e) { 
     Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage()); 
     releaseMediaRecorder(); 
     return false; 
    } 
    return true; 
} 
private void startVideo(){ 
mediaRecorder.start() 
} 

此代碼工作正常,如果我的手機(移動設備)上運行,但它在Nexus 7(平板的)註失敗:承上啓下7只有前置攝像頭可用。

這是錯誤和日誌發生:

01-18 19:31:30.597:E/MediaRecorder(25847):啓動失敗:-19

01-18 19:31:31.647 :E/AndroidRuntime(25847):FATAL EXCEPTION:主
01-18 19:31:31.647:E/AndroidRuntime(25847):進程:com.appsplanet.beasting366,PID:25847
01-18 19:31: 31.647:E/AndroidRuntime(25847):java.lang.RuntimeException:啓動失敗。
01-18 19:31:31.647:E/AndroidRuntime(25847):at android.media.MediaRecorder.start(Native Method)
01-18 19:31:31.647:E/AndroidRuntime(25847):at com .appsplanet.beasting366.ActivityVideoRecord.startVideoRecord(ActivityVideoRecord.java:633)
01-18 19:31:31.647:E/AndroidRuntime(25847):at com.appsplanet.beasting366.ActivityVideoRecord.access $ 6(ActivityVideoRecord.java:620 )
01-18 19:31:31.647:E/AndroidRuntime(25847):at com.appsplanet.beasting366.ActivityVideoRecord $ 2.onClick(ActivityVideoRecord.java:581)
01-18 19:31:31.647:E/AndroidRuntime(25847):at android.view.View.performClick(View.java:4438)
01-18 19:31:31.647: E/AndroidRuntime(25847):at android.view.View $ PerformClick.run(View.java:18422)
01-18 19:31:31.647:E/AndroidRuntime(25847):at android.os.Handler.handleCallback (Handler.java:733)
01-18 19:31:31.647:E/AndroidRuntime(25847):at android.os.Handler.dispatchMessage(Handler.java:95)
01-18 19:31:31.647 :E/AndroidRuntime(25847):at android.os.Looper.loop(Looper.java:136)
01-18 19:31:31.647:E/AndroidRuntime(25847):at android.app.ActivityThread.main( ActivityThread.java:5017)
01-18 19:31:31.647:E/AndroidRuntime(25847):at java.lang.reflect.Method.invokeNative(Native Method)
01-18 19:31:31.647:E/AndroidRuntime(25847):a t java.lang.reflect.Method.invoke(Method.java:515)
01-18 19:31:31.647:E/AndroidRuntime(25847):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run( ZygoteInit.java:779)
01-18 19:31:31.647:E/AndroidRuntime(25847):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-18 19: 31:31.647:E/AndroidRuntime(25847):at dalvik.system.NativeStart.main(Native Method)

任何人都可以幫助我解決這個問題嗎?

感謝 依禪耆那教

+0

可以在崩潰之前發佈日誌嗎? ActivityVideoRecord.prepareCameraRecorder()成功完成了嗎?爲什麼日誌顯示'ActivityVideoRecord.startVideoRecord()'而公開的代碼有'private void startVideo()'? 'ActivityVideoRecord.java'的第633行是什麼? –

回答

0

你確定你這裏有正確的條件?

if (frontCam) { 
    camera = CameraHelper.getDefaultFrontFacingCameraInstance(); 
    camId = Camera.CameraInfo.CAMERA_FACING_BACK; 
} else if (rearCam) { 
    camera = CameraHelper.getDefaultBackFacingCameraInstance(); 
    camId = Camera.CameraInfo.CAMERA_FACING_FRONT; 
} 

如果設備只有前面的凸輪比你使用後面。

+0

是的,其實我有翻轉相機的功能。如果有兩臺攝像機存在。如果只有一個攝像頭存在,它不會進入這種狀態。 –

2

不幸的是,Android Camera API不易理解。有兩種INTconstants

public final static int Camera.CameraInfo.CAMERA_FACING_BACK = 0; 
public final static int Camera.CameraInfo.CAMERA_FACING_FRONT = 1; 

並且有希望int cameraId參數方法,如Camera.open()

但使用上述常量Camera.open()CamcorderProfile.get()是錯誤的。所述cameraId是對於第一相機,用於第二,第三,一個如此類推,直到Camera.getNumberOfCameras()-1。可以與特定cameraId檢查照相機的方向如下:

Camera camera = Camera.open(cameraId); 
int facing = camera.getCameraInfo().facing(); 

面對值保證是要麼Camera.CameraInfo.CAMERA_FACING_BACKCamera.CameraInfo.CAMERA_FACING_FRONT

具體而言,在Nexus 7只有一個攝像頭,所以你總是使用cameraId == 0