5
mMediaRecorder = new MediaRecorder();
    // Step 1: Unlock and set camera to MediaRecorder
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);
    // Step 2: Set sources
    // activate this for recording with sound\

    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

    mMediaRecorder.setVideoSize(getMaxSupportedVideoSize().width,getMaxSupportedVideoSize().height);

    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    // Step 4: Set output file
    mMediaRecorder.setOutputFile(getOutputMediaFile("movie"));

    // Step 4: Set output file
    mMediaRecorder.setOutputFile(getOutputMediaFile("movie"));


    // Step 5: Set the preview output
    mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

    mMediaRecorder.setOrientationHint(90);

above code is working fine But, the quality of video is not as same as video i shoot over native android camera, my video recorded using media recorder is poor quality compare to the native one, how can i improve the video quality.

If any one knows help me out.Thanks

Jignesh Jain
  • 1,518
  • 12
  • 28

3 Answers3

3

I'm not a Java/Android dev, I'm using Xamarin and C#, but I had much the same problem, and my solution should be directly applicable (even the syntax is almost identical).

I found that if you're using setCamera (and are previewing what the camera sees before you start up your mediaRecorder) then it won't let you change the quality settings on the mediaRecorder.

And then when you call mediaRecorder.start(), it either crashes or freezes or displays garbage.

Basically, as long as the Camera is previewing, the MediaRecorder won't be allowed to start recording at a different quality to that which the camera already has. You need to

  1. stop the camera preview,
  2. take away its preview surface
  3. assign the camera to the MediaRecorder (with setCamera)
  4. set the MediaRecorder up with the quality you need
  5. then reattach the preview surface

Then when you start recording, everything works as it should.

So in your case, just before you call mediaRecorder.setCamera(), try the following:

mCamera.stopPreview(); mCamera.setPreviewDisplay(null);

Then further down, do your

mRecorder.setCamera()

That was the solution for me. I can now set the video quality to 720p (or 1080p) and it works perfectly.

However, when you stop recording, your previewing will also stop.

You'll probably need to reinstate your

mCamera.setPreviewDisplay(mPreview.getHolder().getSurface())

to what it was before, and restart the actual preview.

I hope it works for you too :)

wislon
  • 723
  • 5
  • 21
  • @JigneshJain please mark it as the answer, if it did solve your problem? :) – wislon Dec 23 '14 at 12:00
  • @ozwilson! I am trying to record a video and upload it server. Also i want to enable the user to play the recorded video and retake it if he doesn't like it. Here's my code. http://forums.xamarin.com/discussion/31400/record-video-in-xamarin-android-using-front-facing-camera#latest. I tried to to play around with the sample code http://developer.xamarin.com/recipes/android/media/video/record_video/ , but to no result – Hemant Bavle Jan 22 '15 at 09:54
1

Alternative 1

recorder.setVideoSize(640, 480);
recorder.setVideoFrameRate(16); //might be auto-determined due to lighting
recorder.setVideoEncodingBitRate(3000000);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);// MPEG_4_SP
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

Alternative 2

CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
Jeff Bootsholz
  • 2,971
  • 15
  • 70
  • 141
1

Replace this code:

    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

mMediaRecorder.setVideoSize(getMaxSupportedVideoSize().width,getMaxSupportedVideoSize().height);

mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

with :

    mMediaRecorder.setProfile(CamcorderProfile
            .get(CamcorderProfile.QUALITY_HIGH));
abhishek kumar gupta
  • 2,189
  • 6
  • 35
  • 56