2

I am getting below excetion while trying to record video from front camera.However back camera recording works fine.Crash is on line mMediaRecorder.start();

java.lang.RuntimeException: start failed.
       at android.media.MediaRecorder.start(MediaRecorder.java)
       at xyz.CameraFragment$6.onClick(CameraFragment.java:270)
       at android.view.View.performClick(View.java:4466)
       at android.view.View$PerformClick.run(View.java:18537)
       at android.os.Handler.handleCallback(Handler.java:733)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5102)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
       at dalvik.system.NativeStart.main(NativeStart.java)

Below is my code-

private boolean prepareVideoRecorder() {
    mMediaRecorder = new MediaRecorder();
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
    mVideoFile = new File(getOutputMediaFile().toString());
    mMediaRecorder.setOutputFile(mVideoFile.getAbsolutePath());
    mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
    mMediaRecorder.setOrientationHint(270);
    mMediaRecorder.setMaxDuration(10000);
    mMediaRecorder.setOnInfoListener(this);
    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        Log.d("CAMERA", "IllegalStateException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        Log.d("CAMERA", "IOException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    }
    return true;
}
if (prepareVideoRecorder()) {
    // Camera is available and unlocked, MediaRecorder is prepared,
    // now you can start recording
    mMediaRecorder.start();
}
  • Possible duplicate of [Android cant record video with Front Facing Camera, MediaRecorder start failed: -19](http://stackoverflow.com/questions/14681703/android-cant-record-video-with-front-facing-camera-mediarecorder-start-failed) – Alex Cohn Oct 20 '15 at 17:31

1 Answers1

2

Based on spitzanator's answer:

  1. Make sure your permissions are correct:

<uses-feature android:name="android.hardware.camera.front" />

  1. Apparently this line of code does not work with the front camera:

mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

spitzanator also says: That signature for CamcorderProfile.get() defaults to a profile for the back-facing camera:

Returns the camcorder profile for the first back-facing camera on the device at the given quality level. If the device has no back-facing camera, this returns null.

The ideal solution can be found here.

Side note: Due to low reputation I wasn't able to make a comment rather than posting an answer, so all the credits goes for spitzanator.

Community
  • 1
  • 1
AuroMetal
  • 928
  • 3
  • 14
  • 32
  • so for front camera what should i replace this line with-`mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));`..The first paramerter hould be the id of the camera that is opened?What is the id of camera and how can i find it? –  Oct 21 '15 at 11:51
  • Hi @Stranger, I do believe this answer is worth to try: [here](http://stackoverflow.com/a/17503007/3812244). There's an attempt to retrive the ID of the camera. Moreover, I'd also suggest you to look into the Android docs for details on the camera [here](http://developer.android.com/guide/topics/media/camera.html) – AuroMetal Oct 21 '15 at 11:59
  • @Stranger, this [link](http://developer.android.com/reference/android/hardware/Camera.CameraInfo.html) seems to be useful too. – AuroMetal Oct 21 '15 at 12:02
  • `recorder.setProfile(CamcorderProfile.get(cameraId, CamcorderProfile.QUALITY_HIGH));` works... specifying the camera id works. – Pierre Aug 26 '19 at 05:42