2

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);

日志猫:

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)
4

1 回答 1

0

AudioRecord.getMinBufferSize不支持PCM_8BIT(请参阅AudioRecord 源代码)ERROR_BAD_VALUE ,如果您将PCM_8BIT格式传递给它,将返回(-2)。

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

于 2013-03-26T10:17:53.690 回答