2013-03-26 83 views
0

當使用AudioFormat.ENCODING_PCM_8BIT作爲AudioFormat時,我遇到以下問題。當使用AudioFormat.ENCODING_PCM_16BIT這些工作正常。AudioRecord - 無效的音頻緩衝區大小

bufferSize = AudioRecord.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_8BIT); 
audio = new AudioRecord(MediaRecorder.AudioSource.CAMCORDER, sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_8BIT, bufferSize); 

的logcat:

03-26 10:03:49.643: E/AndroidRuntime(19949): FATAL EXCEPTION: main 
03-26 10:03:49.643: E/AndroidRuntime(19949): java.lang.IllegalArgumentException: Invalid audio buffer size. 
03-26 10:03:49.643: E/AndroidRuntime(19949): at android.media.AudioRecord.audioBuffSizeCheck(AudioRecord.java:340) 
03-26 10:03:49.643: E/AndroidRuntime(19949): at android.media.AudioRecord.<init>(AudioRecord.java:237) 
03-26 10:03:49.643: E/AndroidRuntime(19949): at com.kris.test.SoundMeter.runForCamMic(SoundMeter.java:57) 
03-26 10:03:49.643: E/AndroidRuntime(19949): at com.kris.test.VolumeCalc.run(VolumeCalc.java:89) 
03-26 10:03:49.643: E/AndroidRuntime(19949): at com.kris.test.StateChecker.micCalibration(StateChecker.java:159) 
03-26 10:03:49.643: E/AndroidRuntime(19949): at com.kris.test.Settings$3.onClick(Settings.java:354) 
03-26 10:03:49.643: E/AndroidRuntime(19949): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167) 
03-26 10:03:49.643: E/AndroidRuntime(19949): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-26 10:03:49.643: E/AndroidRuntime(19949): at android.os.Looper.loop(Looper.java:137) 
03-26 10:03:49.643: E/AndroidRuntime(19949): at android.app.ActivityThread.main(ActivityThread.java:4921) 
03-26 10:03:49.643: E/AndroidRuntime(19949): at java.lang.reflect.Method.invokeNative(Native Method) 
03-26 10:03:49.643: E/AndroidRuntime(19949): at java.lang.reflect.Method.invoke(Method.java:511) 
03-26 10:03:49.643: E/AndroidRuntime(19949): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 
03-26 10:03:49.643: E/AndroidRuntime(19949): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 
03-26 10:03:49.643: E/AndroidRuntime(19949): at dalvik.system.NativeStart.main(Native Method) 
+0

'bufferSize'的值是什麼? – Michael 2013-03-26 10:08:20

回答

0

AudioRecord.getMinBufferSize不支持PCM_8BIT(見AudioRecord source code),並且將返回ERROR_BAD_VALUE(-2),如果你傳遞一個PCM_8BIT格式它

一個解決辦法。要切換到使用PCM_16BIT編碼另一種可能性(我沒有驗證它是否有效)是根據要求最小緩衝區大小並在構造AudioRecord實例時將返回的大小除以2。