MediaRecorder setVideoSize shows different behavior on different devices

I am using a media recorder to record video in an Android application.

mMediaRecorder.setCamera(mServiceCamera); mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); //mMediaRecorder.setVideoSize(mPreviewSize.width, mPreviewSize.height); mMediaRecorder.setVideoFrameRate(30); mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); String file_name = Environment.getExternalStorageDirectory().getPath() +"/myVideo.mp4"; mMediaRecorder.setOutputFile(file_name); mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); mMediaRecorder.prepare(); mMediaRecorder.start(); 

The problem is the line

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

On HTC and Xperia, setVideoSize works fine (it will work only if I don't comment on this line). But on Nexus and Note, setVideoSize will not work (will work only if I comment on this line).

What to do so that the application runs on all of these devices correctly?

+8
android camera mediarecorder
source share
2 answers

You need to understand that the preview and the actual captured video are two different things, as are the sizes of the preview, and the sizes of the video are two different parameters. What you see in the viewfinder is essentially a preview, but that’s not what it actually records.

  • When you start the camera, you set the size of the preview on the camera. But you must request the supported preview sizes and set one of them.

    Camera camera = camera.open (); List psizes = camera.getParameters () .getSupportedPreviewSizes ();

  • Once you set up the preview, you can start recording using MediaRecorder, and the video size can be set to the media recorder, and this will be the actual size of the video that will be shot. Again, you should set one of the supported video sizes.

    List sizes = camera.getParameters () .getSupportedVideoSizes ();

and then you can install one of them on the media recorder

 mediaRecorder.setVideoSize(videoWidth, videoHeight); 

So, do not forget to always check the supported sizes, otherwise you will definitely get application crashes.

+4
source share

The video sizes in the device are equal to the preview sizes. You must first check if the size of the video you are setting is available or not. Video sizes on different devices may vary. First check the available preview sizes using getSupportedPreviewSizes () , and then set the video size. this will return the list. You must select only one of them.

0
source share

All Articles