2014-05-23 26 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

相關問題