2

我正在尝试使用AudioRecord,但我无法正确初始化录制。我有两台设备,其中一台运行良好,但另一台一直出现异常。

我的代码:

bufferSize = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT);
AudioRecord recorder = new AudioRecord(AudioSource.DEFAULT, rate,channelConfig, audioFormat, bufferSize);

为什么这是错误的,正确的做法是什么?

4

2 回答 2

2
    private int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 };
    int bufferSize;

    AudioRecord audioInput = findAudioRecord();

    public AudioRecord findAudioRecord() {
        for (int rate : mSampleRates) {
            for (short audioFormat : new short[] {
                    AudioFormat.ENCODING_PCM_8BIT,
                    AudioFormat.ENCODING_PCM_16BIT }) {
                for (short channelConfig : new short[] {
                        AudioFormat.CHANNEL_IN_MONO,
                        AudioFormat.CHANNEL_IN_STEREO }) {
                    try {
                        Log.d("Mic2", "Attempting rate " + rate
                                + "Hz, bits: " + audioFormat
                                + ", channel: " + channelConfig);
                        bufferSize = AudioRecord.getMinBufferSize(rate,
                                channelConfig, audioFormat);

                        if (RECORDINGDURATION * sampleRate != AudioRecord.ERROR_BAD_VALUE) {
                            // check if we can instantiate and have a
                            // success
                            AudioRecord recorder = new AudioRecord(
                                    AudioSource.DEFAULT, rate,
                                    channelConfig, audioFormat, bufferSize);
                            if (recorder.getState() == AudioRecord.STATE_INITIALIZED)
                                sampleRate = rate;
                            return recorder;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, rate + "Exception, keep trying.", e);
                    }
                }
            }
        }
        return null;
    }

该设备很可能不支持 16 位编码 -> AudioFormat.ENCODING_PCM_16BIT :-)

于 2012-10-26T15:13:12.020 回答
2

模拟器不支持 CHANNEL_CONFIGURATION_STEREO & sampleRate = (11025, 16000, 22050, and 44100),如果你想成功运行,那么使用 CHANNEL_CONFIGURATION_MONO & SampleRate =8000(它只支持每秒 8000 个样本)。

以下是有关操作的详细说明:http: //developer.android.com/guide/topics/media/audio-capture.html

于 2012-10-26T15:13:28.757 回答