Search code examples
androidandroid-mediarecordersurfaceholder

Can't set video quality for media recorder.video produces flickering video


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


Solution

  • 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 :)