2013-07-23 54 views
11

下面是我工作的代碼結構來記錄視頻和音頻:採用Android MediaRecorder

問題: 1)爲什麼CamcorderProfile需要? setProfile(...)似乎設置爲任何QUALITY_HIGH給人的尺寸,但後來我把我想要的尺寸與setVideoSize(...),它覆蓋了這一點。然而,當我的logcat E/MediaRecorder(19526): setVideoSize called in an invalid state: 2卸下兩個CamcorderProfile線,應用程序崩潰的setVideoSize(...)

2)我怎麼不能錄製音頻?該文件指出,如果setAudioSource(...)未被調用,則不會有音軌。然而,當我刪除了這一行的應用程序崩潰的setProfile(...)與logcat的E/MediaRecorder(19946): try to set the audio encoder without setting the audio source first

3)如果刪除兩個CamcorderProfile線和setAudioSource(...)線,它崩潰的步驟1)。

4)我還嘗試添加行

recorder.setOutputFormat(OutputFormat.DEFAULT); 

代替CamcorderProfile線。但現在它崩潰在perpare()。如果setAudioSource(...)被稱爲logcat的是:E/MediaRecorder(20737): audio source is set, but audio encoder is not set,如果它不叫logcat的是:E/MediaRecorder(20544): video source is set, but video encoder is not set

我已經看過印花布互聯網,我無法找到正確的方式來設置MediaRecorder一個很好的例子。 Here它意味着在API 8之後,您應該使用CamcorderProfile類,但是在我看來這是造成問題的原因。

任何幫助將是偉大的!謝謝!

代碼(當它運行它的工作原理是如下圖):

recorder = new MediaRecorder(); 
recorder.setCamera(<<camera>>); 
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); 
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); 

CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); 
recorder.setProfile(profile); 

recorder.setOutputFile(<<Path String>>); 
recorder.setVideoSize(<<Width>>, <<Height>>); 

recorder.setPreviewDisplay(<<Surface>>); 

recorder.setOrientationHint(0); 
recorder.setMaxDuration(10000); 
recorder.setOnInfoListener(this); 

try 
{ 
    recorder.prepare(); 
    recorder.start(); 
} catch ... 

回答

15

我沒有很多與MediaRecorder經驗,但我讀了一些相關的話題,我將盡力爲您解答:

1,3和4) CamcorderProfile設置不僅僅是分辨率以上,還設置事情輸出格式和編碼器(用於音頻和視頻)。你所得到的錯誤,因爲你可能需要調用setVideoSize前使用setOutputFormat,你必須調用setVideoEncodersetAudioEncoder後,如果您不想使用CamcorderProfile。 [根據這個answer]

2)再次,CamcorderProfile還設置音頻屬性(如編碼,比特率,採樣率,...),所以你需要調用它之前設置的音頻源,這就是爲什麼該應用程序崩潰。如果您不想要錄製的音頻嘗試下面的代碼:(我沒有測試它,所以我真的不知道,如果它的工作原理,但我敢肯定它)

recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); 
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
recorder.setVideoSize(WIDTH, HEIGHT); 
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); 
recorder.setOutputFile(PATH); 
recorder.setPreviewDisplay(SURFACE); 

recorder.prepare(); 
recorder.start(); 

另外要注意如果您不想使用CamcorderProfile(意味着您只想錄製音頻或視頻),則可能需要設置其他參數以確保您具有所需的質量。看看下面的示例代碼:

recorder = new MediaRecorder(); 
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); 
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); 

// Following code does the same as getting a CamcorderProfile (but customizable) 
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
// Video Settings 
recorder.setVideoSize(WIDTH, HEIGHT); 
recorder.setVideoFrameRate(FRAME_RATE); 
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); 
recorder.setVideoEncodingBitRate(VIDEO_BITRATE); 
// Audio Settings 
recorder.setAudioChannels(AUDIO_CHANNELS); 
recorder.setAudioSamplingRate(SAMPLE_RATE); 
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
recorder.setAudioEncodingBitRate(AUDIO_BITRATE); 

// Customizable Settings such as: 
// recorder.setOutputFile(PATH); 
// recorder.setPreviewDisplay(SURFACE); 
// etc... 

// Prepare and use the MediaRecorder 
recorder.prepare(); 
recorder.start(); 
... 
recorder.stop(); 
recorder.reset(); 
recorder.release(); 

我希望這可以幫助你。

+1

你是對的,就像我發佈的那樣,我找到了'setProfile()'函數的代碼。它總是調用'setAudioEncoder()',所以如果你不設置音頻源,它會崩潰。答案是複製這個功能,這樣你仍然可以使用內置的'CamcorderProfile'設置,但也可以選擇你想要設置的設置(即音頻)。謝謝 – jacobianism

+0

是的,如果你想使用攝像機只獲得視頻,你也可以做到這一點。通過你的方式,你可以獲得任何設備的最佳可用值,而無需對它們進行硬編碼。 –

+1

經過大量搜索你的帖子,我的一天。將setVideoSize放在setVideoEncoder和setAudioEncoder之前就可以了。謝謝 – gtsouk