2

I am creating a video recorder functionality for my application. I have used the following code for preparing the camera and media recorder api:

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()
}

This code is working fine if I am running it in phone (mobile device) but it fails on Nexus 7 (Tablet's) Note: nexus 7 has only front camera available.

This is error and logs occured:

01-18 19:31:30.597: E/MediaRecorder(25847): start failed: -19

01-18 19:31:31.647: E/AndroidRuntime(25847): FATAL EXCEPTION: main
01-18 19:31:31.647: E/AndroidRuntime(25847): Process: com.appsplanet.beasting366, PID: 25847
01-18 19:31:31.647: E/AndroidRuntime(25847): java.lang.RuntimeException: start failed.
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): at 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): at 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)

Can anyone help me to solve this issue?

Thanks Ishan jain

Community
  • 1
  • 1
ishan jain
  • 681
  • 3
  • 10
  • 27
  • Can you publish the log before crash? Has `ActivityVideoRecord.prepareCameraRecorder()` finished successfully? Why is the log showing `ActivityVideoRecord.startVideoRecord()` while the exposed code has `private void startVideo()`? What is in line 633 of `ActivityVideoRecord.java`? – Alex Cohn Jan 19 '14 at 11:48

2 Answers2

2

Unfortunately, Android Camera API is not easy to understand. There are two int constants:

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

and there are methods, like Camera.open() that expect int cameraId parameter.

But it is a mistake to use the constants above for Camera.open() or CamcorderProfile.get(). The cameraId is 0 for first camera, 1 for the second, 2 for third, an so on, up to Camera.getNumberOfCameras()-1. You can check the direction of a camera with certain cameraId as follows:

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

The value of facing is guaranteed to be either Camera.CameraInfo.CAMERA_FACING_BACK or Camera.CameraInfo.CAMERA_FACING_FRONT.

Specifically, on Nexus 7 there is only one camera, so you always use cameraId == 0.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
0

Are you sure you have correct condition here?

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

If device have only front cam than you using back.

mik_os
  • 670
  • 1
  • 6
  • 10
  • Yes actually I have functionality to flip the camera. if two cameras are present. And it won't go in this condition if only one camera is present. – ishan jain Jan 18 '14 at 16:54