MediaRecorder setVideoSize shows different behaviour in different devices

时光怂恿深爱的人放手 提交于 2021-02-06 09:12:21

问题


I am using media recorder to record video in an android app.

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 in the line

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

In HTC and Xperia, setVideoSize works fine (Will work only if I don't comment this line). But in Nexus and Note, setVideoSize won't work( Will work only if I comment this line).

What should I do in order for the app to run in all these devices correctly??


回答1:


You need to understand that the preview and the actual captured video are two different things, likewise Preview sizes and Video sizes are two different parameters. What you see in the viewfinder is essentially the preview, but it is not what actually gets recorded.

  1. When starting a camera, you set the preview size to the camera. But you must query for the supported preview sizes and should set one among them.

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

  2. Once you have set up the preview, you can start recording by using a MediaRecorder, and the video size can be set to the media recorder, and it is the actual size of the video that will be captured. Again, you should set one of supported video size.

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

and then, you can set one of these to the media recorder

mediaRecorder.setVideoSize(videoWidth, videoHeight);

So, remember to check for the supported sizes always, else you are bound to get an app crash.




回答2:


Video sizes in a device is equal to preview sizes. You have to first check whether video size you setting is available or not. Video sizes in different devices may be diffrent.so,first check available preview sizes using getSupportedPreviewSizes () and then set video size. this will return a list.you have to select only one of them.



来源:https://stackoverflow.com/questions/16606401/mediarecorder-setvideosize-shows-different-behaviour-in-different-devices

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