【问题标题】:Media recorder setprofile() is not working for nexus phone媒体记录器 setprofile() 不适用于 nexus 手机
【发布时间】:2013-03-13 06:33:52
【问题描述】:

我已经在三星平板电脑上测试了我的代码并注意,但没有相同的代码 致力于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;
}

【问题讨论】:

    标签: android android-camera mediarecorder android-mediarecorder


    【解决方案1】:

    假设您正在尝试最新的Nexus 7,我认为问题可能是QUALITY_HIGH 在您设备的media_profiles.xml未定义。我检查了AOSPhere,发现只有CIFQCIF480p 及其延时版本已被定义。您可以通过查看设备上的media_profiles.xml/etc/media_profiles.xml 来确认这一点。

    您可以调整您的代码以使用CamcorderProfile.QUALITY_480P 而不是CamcorderProfile.QUALITY_HIGH

    【讨论】:

    • 有趣,这显然是 Nexus 7 中的一个错误,因为 CamcorderProfile 类的 Android 文档明确指出:“保证支持质量级别 QUALITY_LOW、QUALITY_HIGH,而其他级别可能会也可能不会得到支持。”
    【解决方案2】:

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

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

    Camera.getCameraInfo(cameraId, info);
    

    如果没有这个调用,你总是会得到 0 来表示朝向和方向(默认整数值)。

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

    CamcorderProfile.get(cameraId, quality);
    

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

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

    【讨论】:

      猜你喜欢
      • 2019-05-06
      • 1970-01-01
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      • 1970-01-01
      • 2018-05-12
      • 1970-01-01
      相关资源
      最近更新 更多