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

时光总嘲笑我的痴心妄想 提交于 2019-12-04 08:01:32

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

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);

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));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!