媒体记录器setprofile()不适用于nexus手机

时间:2013-03-13 06:33:53

标签: android android-camera mediarecorder android-mediarecorder

  

我已在三星平板电脑上测试过我的代码并注意,但相同的代码却没有   致力于nexus。

     

我知道nexus只有前置摄像头,而摄像头正在打开但是   克服开始录制按钮它崩溃....我检查了它   通过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;
}

2 个答案:

答案 0 :(得分:1)

假设您正在尝试最新的Nexus 7,我觉得问题可能是您的设备的QUALITY_HIGHmedia_profiles.xml 未定义。我查看了AOSPhere,发现只定义了CIFQCIF480p及其失时版本。您可以在media_profiles.xml上查看设备上的/etc/media_profiles.xml来确认这一点。

您可以调整代码以使用CamcorderProfile.QUALITY_480P代替CamcorderProfile.QUALITY_HIGH

答案 1 :(得分:0)

我发现您的代码存在一些问题,其中任何一个都可能导致您的错误。

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

Camera.getCameraInfo(cameraId, info);

如果没有此调用,面向和方向(默认整数值)将始终为0。

其次,您还应该使用接受cameraId的方法获取配置文件:

CamcorderProfile.get(cameraId, quality);

我假设您正在使用Camera.open(int cameraId)方法,否则您可能无法使用任何前置摄像头。

另外,这只是惯例,但我建议你将switch语句中的方向代码分解为可读性函数。切换情况理想情况下应该只有一行,但几行仍然没问题。