Android MediaRecorder准备失败,setVideoSize 1080p
我的项目中有一个Android MediaRecorder准备失败,setVideoSize 1080p,android,mediarecorder,Android,Mediarecorder,我的项目中有一个MediaRecorder对象,该对象初始化如下: private void initRecorder() { mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); mMediaRecorder.setOutputFormat(MediaRecorder.O
MediaRecorder
对象,该对象初始化如下:
private void initRecorder() {
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
mMediaRecorder.setVideoFrameRate(60);
mMediaRecorder.setVideoSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);
String filepath_external = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test/";
File f = new File(filepath_external);
if(!f.exists()){
f.mkdirs();
}
mMediaRecorder.setOutputFile(filepath_external + System.currentTimeMillis() + ".mp4");
}
如果我设定
private static int DISPLAY_WIDTH = 1024;
private static int DISPLAY_HEIGHT = 768;
调用initRecorder()
,然后调用mMediaRecorder.prepare()
,一切正常
然而,我想用智能手机屏幕的大小来记录。因此,我使用以下代码设置宽度和高度:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
DISPLAY_WIDTH = size.x;
DISPLAY_HEIGHT = size.y;
现在宽度=1920,高度=1080(基本上是1080p)。结果是我的活动甚至没有启动,产生以下错误:
E/MediaRecorder:准备失败:-2147483648
W/System.err:java.io.IOException:prepare失败
W/System.err:at-android.media.MediaRecorder.\u prepare(本机方法)
W/System.err:at-android.media.MediaRecorder.prepare(MediaRecorder.java:873)
W/System.err:com.lfdversluis.testrecord.RecordActivity.prepareRecorder(RecordActivity.java:161)
为什么它不起作用,我怎样才能使它起作用
如果我在
getSupportedVideoSizes()
中打印所有支持的视频大小,我会得到:
可选尺寸:1920 1080
可选尺寸:14401080
可选尺寸:1280720
可选尺寸:800 450
可选尺寸:720480
可选尺寸:640480
可选尺寸:320 240
可选尺寸:176 144
所以1920到1080应该得到支持
使用如下所示的
CamcorderProfile
,我也得到了1920乘1080的结果。同样,准备功能失败:
CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
int cameraWidth = camcorderProfile != null ? camcorderProfile.videoFrameWidth : -1;
int cameraHeight = camcorderProfile != null ? camcorderProfile.videoFrameHeight : -1;
DISPLAY_WIDTH = cameraWidth;
DISPLAY_HEIGHT = cameraHeight;
Log.e("Campcorder profile", String.format("%s %s", DISPLAY_WIDTH, DISPLAY_HEIGHT));
借助于公共软件,我发现了这个问题
同时确保检查摄像头参数中的
getSupportedPreviewFrameRates
。硬编码60或30 fps(例如在情况下)可能并不总是在每个设备上工作。如果使用CamcorderProfile
,还应将帧速率设置为CamcorderProfile.videoFrameRate
为什么要硬编码值,而不是询问设备支持什么视频分辨率@Commonware,因为这些是摄像头参数,我目前正在拍摄手机屏幕。我知道支持的sizes方法,但我认为在捕捉相机和捕捉设备屏幕方面存在差异。阅读了文档后,我现在看到它显示了支持的视频大小,但奇怪的是,捕获屏幕内容需要遵守相机记录的大小。感谢您展示这一点。“我正在拍摄手机屏幕”——您是说使用媒体投影API?这不在你的问题中,尽管现在我看到了表面
位。Jake Wharton在中使用CamcorderProfile
计算高质量录制选项的屏幕大小,然后使用该屏幕大小确定如何设置实际的屏幕录制。@Commonware,如果我打印所有支持的大小,我会得到1920 x 1080,因此根据您的理论,MediaRecorder应该在这个尺寸上工作,但事实并非如此。当你使用杰克·沃顿的camcoderprofile
方法时,会发生什么?我的意思是,我认为他的方法是有效的,因为他的应用程序在Play Store上。