Reputation: 2662
I record video using MediaRecorder
in a SurfaceView
,once done recording,I pass to another activity to preview the video.I want the video capture is fit perfectly to device screen
I tried to set video size when prepare recorder:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
height = displayMetrics.heightPixels;
width = displayMetrics.widthPixels;
//# Video settings
mRecorder.setVideoSize(width,height);
I also set the camera parameter in surfaceCreated() as well:
mCamera = Camera.open();
Camera.Parameters parameters;
parameters = mCamera.getParameters();
parameters.setPreviewSize(width,height);
mCamera.setParameters(parameters);
Once done recording,I pass the video captured to another activity,and set into a VideoView
,the VideoView
layout already set to MATCH_PARENT
for width and height already.
videoPreview = (VideoView)findViewById(R.id.videoPreview);
videoPreview.setVideoURI(videoUri);
videoPreview.start();
Although I set the video size and camera parameters,but the video capture still not as big as the device screen.I using Pixel XL emulator,the width is fit the screen,but the height is only half of the screen.
So my question is,how to capture a video that will fit perfectly to width and height of every screen using MediaRecorder
?
Upvotes: 1
Views: 4509
Reputation: 1006644
So my question is,how to capture a video that will fit perfectly to width and height of every screen using MediaRecorder?
That is not possible.
First, there is no requirement that the device's camera(s) support a resolution that is equal to the physical resolution of the screen.
Second, your UI may not be taking up the full screen anyway (e.g., exists in a floating window in a freeform multi-window device).
Upvotes: 0
Reputation: 3053
This is my method to fit video with screen :
when init params of the camera do this code :
Camera.Parameters params = mCamera.getParameters();
preferredSize = params.getPreferredPreviewSizeForVideo();
adaptPrefferedVideoSize(params);
params.setPreviewSize(preferredSize.width, preferredSize.height);
adjustVideoSurface(screenWidth, screenHeight,preferredSize.width, preferredSize.height, surfaceView);
//...
and this the the adaptPrefferedVideoSize method :
public void adaptPrefferedVideoSize(Camera.Parameters params) {
Camera.Size sizeW640 = params.getPreviewSize();
Camera.Size sizeW720 = params.getPreviewSize();
Camera.Size sizeW1280 = params.getPreviewSize();
sizeW640.width = 640;
sizeW640.height = 480;
sizeW720.width = 720;
sizeW720.height = 480;
sizeW1280.width = 1280;
sizeW1280.height = 720;
preferredSize = sizeW1280;
/*************************/
if (params.getSupportedVideoSizes() != null) {
if (params.getSupportedVideoSizes().contains(sizeW1280)) {
preferredSize = sizeW1280;
} else if (params.getSupportedVideoSizes().contains(sizeW720)) {
preferredSize = sizeW720;
} else {
preferredSize = sizeW640;
}
} else if (preferredSize != null) {
if (preferredSize.width > sizeW1280.width) {
preferredSize = sizeW1280;
} else if (!preferredSize.equals(sizeW720)) {
preferredSize = sizeW640;
}
} else {
preferredSize = sizeW640;
}
}
and This is adjustVideoSurface method :
public static boolean adjustVideoSurface(int screenWidth,
int screenHeight,
int width,
int height,
SurfaceView surfaceView) {
int preferredWidth = (width < height) ? width : height;
int preferredHeight = (width < height) ? height : width;
float ratioW = (float) screenWidth / preferredWidth;
float ratioH = (float) screenHeight / preferredHeight;
RelativeLayout.LayoutParams layoutParams;
int displayedHeight = (int) (ratioW * preferredHeight);
int displayedWidth = (int) (ratioH * preferredWidth);
layoutParams = new RelativeLayout.LayoutParams(screenWidth, displayedHeight);
if (displayedHeight > screenHeight) {
layoutParams = new RelativeLayout.LayoutParams(displayedWidth, screenHeight);
}
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
surfaceView.setLayoutParams(layoutParams);
surfaceView.requestLayout();
return Math.round(ratioH - ratioW) == 0;
}
I hope that this coul help you.
Upvotes: 0