0

我已在三星平板电脑上测试过我的代码,但请注意,但相同的代码不是 工作在nexus上。媒体记录器setprofile()不适用于nexus手机

我知道nexus只有前置摄像头和相机开放,但 cliking在开始录制按钮它崩溃....我检查了它通过setProfile上的错误 。

我的代码:

public boolean prepareVideoRecorder() { 
     Toast.makeText(context, "Please wait..", Toast.LENGTH_SHORT).show(); 
// mCamera = getCameraInstance(); 
    mMediaRecorder = new MediaRecorder(); 
    // Step 1: Unlock and set camera to MediaRecorder 
    mCamera.stopPreview(); 
    mCamera.unlock(); 
    mMediaRecorder.setCamera(mCamera); 
    // Step 2: Set sources 
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); 
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 
    int layout = getResources().getConfiguration().screenLayout 
       & Configuration.SCREENLAYOUT_SIZE_MASK; 
     if (layout == Configuration.SCREENLAYOUT_SIZE_XLARGE) { 

    switch (((WindowManager) getSystemService(WINDOW_SERVICE)) 
      .getDefaultDisplay().getOrientation()) { 
    case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: { 
    // Toast.makeText(context, "SCREEN_ORIENTATION_LANDSCAPE", Toast.LENGTH_SHORT).show(); 
     int degrees = 0; 
     android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); 
     int result; 
     if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
      result = (info.orientation + degrees) % 360; 
      result = (360 - result) % 360; // compensate the mirror 
    //  Toast.makeText(context, "SCREEN_ORIENTATION_LANDSCAPE 1111", Toast.LENGTH_SHORT).show(); 
     } else { // back-facing 
      result = (info.orientation - degrees + 360) % 360; 
    //  Toast.makeText(context, "SCREEN_ORIENTATION_LANDSCAPE 222-- "+ result, Toast.LENGTH_SHORT).show(); 
     } 

     mMediaRecorder.setOrientationHint(result); 

     break; 
    } 
    case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: { 
    // Toast.makeText(context, "SCREEN_ORIENTATION_PORTRAIT", Toast.LENGTH_SHORT).show(); 
     int degrees = 270; 
     android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); 
     int result; 
     if (CurrentValues.cur_cam_face == 1) { 
     // Toast.makeText(context, "SCREEN_ORIENTATION_PORTRAIT 111", Toast.LENGTH_SHORT).show(); 
      result = (info.orientation + degrees) % 360; 
      result = (360 - result) % 360; // compensate the mirror 
      // System.out.println("FRONT CAMERA)))))))"); 

     } else { // back-facing 
     // Toast.makeText(context, "SCREEN_ORIENTATION_PORTRAIT 222", Toast.LENGTH_SHORT).show(); 
      result = degrees;// (info.orientation - degrees + 360) % 360; 
      // System.out.println("BACK CAMERA)))))))"); 
     } 

     mMediaRecorder.setOrientationHint(result); 
     break; 
    } 
    case Configuration.ORIENTATION_LANDSCAPE: { 
    // Toast.makeText(context, "ORIENTATION_LANDSCAPE", Toast.LENGTH_LONG).show(); 
     int degrees = 180; 
     android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); 
     int result; 
     if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
     // Toast.makeText(context, "ORIENTATION_LANDSCAPE 111", Toast.LENGTH_SHORT).show(); 
      result = (info.orientation + degrees) % 360; 
      result = (360 - result) % 360; // compensate the mirror 
     } else { // back-facing 
     // Toast.makeText(context, "ORIENTATION_LANDSCAPE 22", Toast.LENGTH_SHORT).show(); 
      result = (info.orientation - degrees + 360) % 360; 
     } 

     mMediaRecorder.setOrientationHint(result); 
     break; 
    } 
    default: { 
     //Toast.makeText(context, "DEFAULT 11", Toast.LENGTH_SHORT).show(); 
     int degrees = 90; 
     android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); 
     int result; 
     if (CurrentValues.cur_cam_face == 1) { 
      result = (info.orientation + degrees) % 360; 
      result = (360 - result) % 360; // compensate the mirror 
     } else { // back-facing 
      result = degrees;// (info.orientation - degrees + 360) % 360; 
     } 

     mMediaRecorder.setOrientationHint(result); 
    } 

     } 
     }else if (layout == Configuration.SCREENLAYOUT_SIZE_LARGE) { 
      switch (((WindowManager) getSystemService(WINDOW_SERVICE)) 
        .getDefaultDisplay().getOrientation()) { 

      case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: { 
       Toast.makeText(context, "PORTRAIT " + ActivityInfo.SCREEN_ORIENTATION_PORTRAIT, Toast.LENGTH_SHORT).show(); 
       int degrees = 0; 
       android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); 
       int result; 
       if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
        result = (info.orientation + degrees) % 360; 
        result = (360 - result) % 360; // compensate the mirror 
       Toast.makeText(context, "SCREEN_PORTRAIT 1111", Toast.LENGTH_SHORT).show(); 
       } else { // back-facing 
        result = (info.orientation - degrees + 360) % 360; 
        Toast.makeText(context, "SCREEN_PORTRAIT 222-- "+ result, Toast.LENGTH_SHORT).show(); 
       } 
       mMediaRecorder.setOrientationHint(result); 
       break; 
      } 
      case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: { 


       Toast.makeText(context, "else LANDSCAPE " +ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE, Toast.LENGTH_SHORT).show(); 
       int degrees = 90; 
       android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); 
       int result; 
       if (CurrentValues.cur_cam_face == 1) { 
        result = (info.orientation + degrees) % 360; 
        result = (360 - result) % 360; // compensate the mirror 
       } else { // back-facing 
        result = degrees;// (info.orientation - degrees + 360) % 360; 
       } 
       mMediaRecorder.setOrientationHint(result); 

       break; 
     } 
      } 

    }else 
     { 
      switch (((WindowManager) getSystemService(WINDOW_SERVICE)) 
        .getDefaultDisplay().getOrientation()) { 
      case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: { 
       Toast.makeText(context, "PORTRAIT " + ActivityInfo.SCREEN_ORIENTATION_PORTRAIT, Toast.LENGTH_SHORT).show(); 
       int degrees = 0; 
       android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); 
       int result; 
       if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
        result = (info.orientation + degrees) % 360; 
        result = (360 - result) % 360; // compensate the mirror 
       Toast.makeText(context, "SCREEN_PORTRAIT 1111", Toast.LENGTH_SHORT).show(); 
       } else { // back-facing 
        result = (info.orientation - degrees + 360) % 360; 
        Toast.makeText(context, "SCREEN_PORTRAIT 222-- "+ result, Toast.LENGTH_SHORT).show(); 
       } 
       mMediaRecorder.setOrientationHint(result); 
       break; 
      } 
      case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: { 


       Toast.makeText(context, "else LANDSCAPE " +ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE, Toast.LENGTH_SHORT).show(); 
       int degrees = 90; 
       android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); 
       int result; 
       if (CurrentValues.cur_cam_face == 1) { 
        result = (info.orientation + degrees) % 360; 
        result = (360 - result) % 360; // compensate the mirror 
       } else { // back-facing 
        result = degrees;// (info.orientation - degrees + 360) % 360; 
       } 
       mMediaRecorder.setOrientationHint(result); 

       break; 
     } 
      } 
     } 

    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher) 
    CamcorderProfile camcorderProfile_HQ; 
    if (CurrentValues.cur_cam_face == 1) 
     camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW); 
    else 
     camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); 
    mMediaRecorder.setProfile(camcorderProfile_HQ); 
    // Step 4: Set output file 
    mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO) 
      .toString()); 
    // Step 5: Set the preview output 
    mMediaRecorder.setPreviewDisplay(mPreview.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; 
} 

回答

1

假设你想在最新的Nexus 7,我觉得这个问题可能是QUALITY_HIGH不是在你的设备的media_profiles.xml定义。我检查了AOSPhere,发现只有CIF,QCIF,480p及其时间推移的版本已被定义。您可以通过在您的设备上查看media_profiles.xml来确认此事,网址为/etc/media_profiles.xml

您可以修改代码以使用CamcorderProfile.QUALITY_480P而不是CamcorderProfile.QUALITY_HIGH

+1

有趣的是,这显然是Nexus 7中的一个错误,因为CamcorderProfile类的Android文档明确指出:“质量等级QUALITY_LOW,QUALITY_HIGH保证受支持,而其他等级可能不支持。” – Nantoka 2014-04-14 18:50:06

0

我看到你的代码有几个问题,其中任何一个都可能导致你的错误。

首先,刚刚创建CameraInfo对象是不够的,你必须调用:

Camera.getCameraInfo(cameraId, info); 

没有这个电话,你永远只得到0面临和方向(默认整数值)。

其次,你也应该得到与方法接受cameraId轮廓:

CamcorderProfile.get(cameraId, quality); 

我假设你正确使用Camera.open(INT cameraId)方法,或者你可能不会让任何前置摄像头工作。

此外,这只是惯例,但我建议你将开关语句中的方向代码分解为函数以提高可读性。理想情况下,开关盒应该只有一条线,但几条线仍然可以。