0

Samsung タブレットでコードをテストしてメモしましたが、同じコードが 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;
}
4

2 に答える 2

1

最新の を試していると仮定すると、問題はデバイスの で定義されていない可能Nexus 7性があるQUALITY_HIGHと思います。ここでツリーを確認したところ、 、、およびそれらのタイムラプス バージョンのみが定義されていることがわかりました。これは、デバイスで を表示することで確認できます。media_profiles.xmlAOSPCIFQCIF480pmedia_profiles.xml/etc/media_profiles.xml

CamcorderProfile.QUALITY_480Pの代わりに使用するようにコードを調整できますCamcorderProfile.QUALITY_HIGH

于 2013-03-14T17:16:11.587 に答える
0

あなたのコードにはいくつかの問題があり、そのいずれかがエラーの原因となっている可能性があります。

まず、CameraInfo オブジェクトを作成するだけでは十分ではなく、以下を呼び出す必要があります。

Camera.getCameraInfo(cameraId, info);

この呼び出しがないと、常に向きと方向 (デフォルトの整数値) に対して 0 が返されます。

次に、cameraId を受け入れるメソッドを使用してプロファイルも取得する必要があります。

CamcorderProfile.get(cameraId, quality);

Camera.open(int cameraId) メソッドを適切に使用しているか、前面カメラを動作させていない可能性があると想定しています。

また、これは単なる慣例ですが、読みやすくするために、switch ステートメントのオリエンテーション コードを関数に分割することをお勧めします。Switch ケースは理想的には 1 行だけにする必要がありますが、数行でも問題ありません。

于 2013-05-29T20:46:56.727 に答える