2014-05-23 22 views
1

当我尝试为MediaRecorder设置视频大小时,我在start方法中得到RuntimeException。MediaRecorder.setVideoSize()在MediaRecorder.start()会导致RuntimeException(启动失败)

mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
if (isVideo) 
    mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 

mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 

mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); 
if (isVideo) { 
    mRecorder.setVideoSize(480, 360); // Works fine when this is removed 
    mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); 
} 

mRecorder.setOutputFile(newFilePath); 

if (isVideo) 
    mRecorder.setPreviewDisplay(surfaceHolder.getSurface()); 

mRecorder.prepare(); // Prepare recorder 
mRecorder.start();  // Start recording 
+0

是否照相机实际支持的大小? – CommonsWare

+0

Welp,可能不是。我将如何去确保大小的支持? –

回答

3

这里就是我发现在我的情况下,最优雅的解决方案:

CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); 
mRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight); 
2

使用getSupportedVideoSizes() on Camera.Parameters获得支持的视频尺寸。相机经常不能随意缩放图像,这大概是出于性能原因。

+0

我用我认为可行的东西更新了我的代码。 getPreferredPreviewSizeForVideo()不是我应该使用的?如果是这样,我怎么知道哪些支持的视频大小使用?返回的列表是从最小到最大排列的? –

+1

@JoshBjelovuk:“getPreferredPreviewSizeForVideo()不是我应该使用的吗?” - 没有。 “如果是这样,我怎么知道哪些支持的视频大小使用?” - 您的决定标准取决于您。 “返回的列表是从最小到最大的顺序排列的?” - 我不会指望它,但你可以写一个“比较器”来帮助。我有一个在我的CWAC相机库中浮动:https://github.com/commonsguy/cwac-camera/blob/master/camera/src/com/commonsware/cwac/camera/CameraUtils.java#L192 – CommonsWare

相关问题