AudioRecord - 音频缓冲区大小无效

时间:2013-03-26 09:28:16

标签: android audiorecord audioformat

使用AudioFormat.ENCODING_PCM_8BIT作为AudioFormat时,我遇到以下问题。使用AudioFormat.ENCODING_PCM_16BIT时,这些工作正常。

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)

1 个答案:

答案 0 :(得分:0)

AudioRecord.getMinBufferSize不支持PCM_8BIT(请参阅AudioRecord source code),如果您向其传递ERROR_BAD_VALUE格式,则会返回PCM_8BIT( - 2)。

一种解决方案是切换到使用PCM_16BIT编码。另一种可能性(我还没有验证它是否有效)是根据PCM_16BIT要求最小缓冲区大小,并在构造AudioRecord实例时将返回的大小除以2。