0

我尝试使用如下录音机方法录制声音,在 HTC nexus one android 2.3 中很好,但是当我在 LG 和 Sony 上使用 Android 4.0 和 4.1 尝试它时,它可以工作一次,下次尝试时它会在 startRecording 中抛出异常有java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.什么问题?

final int RECORDER_BPP = 16;
    int RECORDER_SAMPLERATE = 16000;
    int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
    int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
    int bufferSizeInBytes = AudioRecord
            .getMinBufferSize(RECORDER_SAMPLERATE, RECORDER_CHANNELS,
                    RECORDER_AUDIO_ENCODING);
    // Initialize Audio Recorder.
    AudioRecord audioRecorder = new AudioRecord(
            MediaRecorder.AudioSource.MIC, RECORDER_SAMPLERATE,
            RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, bufferSizeInBytes);

    audioRecorder.startRecording();
4

2 回答 2

0

问题是因为没有使用 release(),在停止记录器后应该释放它,以便您可以多次使用记录器。

于 2013-10-23T07:55:52.977 回答
0

不同的设备仅支持AudioRecord. 有一项功能可让您检测特定设备支持哪些设置。

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

    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(TAG, "Attempting rate " + rate + "Hz, bits: " + audioFormat + ", channel: " + channelConfig);
                        int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);

                        if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {
                            // check if we can instantiate and have a success
                            AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize);

                            if (recorder.getState() == AudioRecord.STATE_INITIALIZED) {
                                return recorder;
                            }
                        }
                    } catch (Exception e) {
                        Log.e(TAG, rate + "Exception, keep trying.", e);
                    }
                }
            }
        }
        return null;
    }

它遍历所有可用音频格式设置的组合,并首先返回匹配您的设备功能。
您可以对其稍作修改,甚至获得设备支持的所有格式。

于 2013-10-22T12:32:50.577 回答