Record video with a different preview size than the resulting video file Record video with a different preview size than the resulting video file android android

Record video with a different preview size than the resulting video file


a.Assume the user sets the size of x,y as video size

b.Now with getSupportedVideoSizes function get the entire list and see if x,y falls in one of them and set the MediaRecorder.setVideoSize().If x,y does not fall in the getSupportedVideoSizes list,then set the default profile for the video record.

This is about the video size

Now coming to the preview size,Not much workaround options.Take a RelativeLayout which holds the SurfaceView.

<android.view.SurfaceView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/preview" android:layout_width="fill_parent" android:layout_height="fill_parent" />

preview is the name of the SurfaceView.Here i have given a sample of re-sizing it to half of the width and height.

resetCamera();  //reset the camera   ViewGroup.LayoutParams params = preview.getLayoutParams();RelativeLayout myRelLayout = (RelativeLayout) findViewById(R.id.myRelLayout);params.width = (int) (myRelLayout.getWidth()/2);params.height = (int)(myRelLayout.getHeight()/2);preview.setLayoutParams(params);initCamera(); //initiate the camera(open camera, set parameter, setPreviewDisplay,startPreview)

please look at the resolution of the preview and then scale down the height or width accordingly based on the video size.

Hope it helps.


As you mention, this is only possible when getSupportedVideoSizes() returns a non-null list.

But if you do see a non-null list, then this simple approach should work:

  1. Set the desired preview resolution with setPreviewSize; the size you select has to be one of the sizes given from getSupportedPreviewSizes.

  2. Set the preview display to your SurfaceView or SurfaceTexture with setPreviewDisplay or setPreviewTexture, respectively.

  3. Start preview.

  4. Create the media recorder, and set its video size either directly with setVideoSize using one of the sizes from getSupportedVideoSizes, or use one of the predefined Camcorder profiles to configure all the media recorder settings for a given quality/size.

  5. Pass the camera object to MediaRecorder's setCamera call, configure the rest of the media recorder, and start recording.

On devices with a non-null getSupportedVideoSizes list, this should result in preview staying at the resolution set by your setPreviewSize call, with recording operating at the set video size/camcorder profile resolution. On devices with no supported video sizes, the preview size will be reset by the MediaRecorder to match the recording size. You should be able to test this by setting a very low preview resolution and a high recording resolution (say, 160x120 for preview, 720p for recording). It should be obvious if the MediaRecorder switches the preview resolution to 720p when recording starts, as the preview quality will jump substantially.

Note that the preview size is not directly linked to the dimensions of the display SurfaceView; the output of the camera preview will be scaled to fit into the SurfaceView, so if your SurfaceView's dimensions are, say 100x100 pixels due to your layout and device, whatever the preview resolution you use will be scaled to 100x100 for display. So you still need to make sure to keep the SurfaceView's aspect ratio correct so that the preview is not distorted.

And for power efficiency, you should not use a preview resolution much higher than the actual number of pixels in your SurfaceView, since the additional resolution will be lost in fitting the preview in the surfaceview. This is of course only possible for recording when getSupportedVideoSizes() returns a non-null value.


First, I will try to answer your specific questions.

  1. it is possible to provide a preview with a different resolution than the actual video. Is this understanding correct?

Yes, preview size is more often than not different from recording size. Preview size is more often than not linked to your display size. So if a phone has display of CIF (352 x 288), but is capable of recording D1 (720 x 480), then preview size and recording size will be different. I feel that other experts have answered sufficiently on this point.

  1. Do some phones allow the behavior and others not?

Most of the latest phones support this feature except maybe a few low-end ones.

Along with setPreviewDisplay, we have to consider this point also:

The one exception is that if the preview surface is not set (or set to null) before startPreview() is called, then this method may be called once with a non-null parameter to set the preview surface. (This allows camera setup and surface creation to happen in parallel, saving time.) The preview surface may not otherwise change while preview is running.

Could you please share the issue faced by you when setPreviewDisplay is invoked with a NULL surface?