The reason for the small scaling when calling MediaRecorder.start () is to change the size of the camera preview in accordance with the resolution of the recorded video. This problem can be fixed by setting the preview and video recovery during setup. I think I also found a way to stop the flicker, although I found that when working with Camera and MediaRecorder, a little lag or flicker can come from any of several places, so it can be a bit harder to track. The documentation for Android on setting up the camera / VCR is a good place to start, so that the main parts are set up correctly, but I found that you need to delve into some of the api class documentation for debugging and make the work really smooth.
The Camera.Parameters
class is the key to maintaining smooth video recording. When you have a Camera object, you can use Camera.getParameters()
to get the current parameters to change them. Camera.setParameters(Camera.Parameters)
can then be used to trigger any changes that have been made.
To prevent video resizing, we need to make sure that the size of the parameter preview matches the recorded video resolution. To get a list of supported video / preview sizes, we can use Camera.Parameters.getSupportedPreviewSizes()
in our current Parameters object, which will return a list of Camera.Size
objects. Each of these objects will have the width and height property, accessed directly through Camera.Size.width
and Camera.Size.height
(without getter methods). The getSupportedPreviewSizes()
method guarantees the return of at least one result , and it seems that the results are ordered from the highest resolution to the lowest.
(For API level> 11, there is also a getSupportedVideoSizes()
method, but it seems that only if the device has some video sizes that differ from the preview sizes, otherwise it returns null. I have not had success with this method, so I will stick with using PreviewSizes for now, as it guarantees returning a size suitable for both video and preview, but this is what you need to know about promotion.)
Once we have Camera.Size
that matches the desired video resolution, we can set this size using Camera.Parameters.setPreviewSize(width, height).
Also, to help flicker, I use Camera.Parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO).
. These steps are performed, use Camera.setParameters()
to notify the camera of your changes. I had success setting these parameters immediately after receiving the camera, since the settings when the user interacts with this action caused some lag. If you use the same operation to capture video and images, you can also set image parameters here, the camera object will process using the appropriate parameters for each mode.
Almost done! Now that the preview has taken care, all that remains is to make sure that MediaRecorder
uses the same resolution as the preview. When preparing a media recorder between calls to MediaRecorder.setProfile()
(or setting encoders, for API level <8) and MediaRecorder.setOutputFile()
place the call on MediaRecorder.setVideoSize(width, height)
using the same values โโas your preview. Now the transition from preview to recording using MediaRecorder.start()
should be seamless, as they both use the same resolution.
Here are some quick snippets of code so you can see everything in action:
Getting and setting parameters:
Camera.Parameters params = camera.getParameters(); params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
Then set the size on the media recorder: