AudioRecord start()错误状态-38

时间:2015-01-28 23:56:21

标签: android android-audiorecord

我正在尝试设置录音机,但我一直遇到一个特定错误,我无法弄清楚原因。在我的代码中,我已经检查了录音机在startrecording()方法之前和之后的状态。

ar = new AudioRecord(audiosource, sampleRate, channelConfiguration,
                audioEncoding, buffersizebytes);
Log.d("info", "ar.getState() before = " + String.valueOf(ar.getState()));
ar.startRecording();
Log.d("info", "ar.getState() after = " +String.valueOf(ar.getState()));

当我运行应用程序时,我在logcat中收到这些消息。

D/info﹕ ar.getState() before = 1
E/AudioRecord﹕ start() status -38
D/info﹕ ar.getState() after = 1

根据我在文档中看到的,状态1可以对应于处于RECORDSTATE_STOPPED或STATE_INITIALIZED状态的录音机,这两个状态在调用startRecording()时都不会导致问题。

状态-38我认为是/ *函数未实现的errno.h代码* /这是指startRecording()方法,或者它在错误中指出start()函数,它不是AudioRecorder类的方法。

我已经尝试了多种方法来确保调用了release()函数,所以我认为这不是问题所在
非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

好吧所以这就是我认为我需要做的事情应该在AudioRecorder文档中真正讨论过,但这个类似的问题让我得到答案AudioRecord object not initializing 基本上你想要做的是循环所有的配置,并尝试每个配对AudioRecord.ERROR_BAD_VALUE检查也因为我计划使用fft,其中长度必须是2的幂我添加一点如果否则如果任何人的部分否则会遇到类似情况

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("audioSetup", "Attempting rate " + rate + "Hz, bits: " + audioFormat + ", channel: " + channelConfig);
                    int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);
                    if (bufferSize > 0 && bufferSize <= 256){
                        bufferSize = 256;
                    }else if (bufferSize > 256 && bufferSize <= 512){
                        bufferSize = 512;
                    }else if (bufferSize > 512 && bufferSize <= 1024){
                        bufferSize = 1024;
                    }else if (bufferSize > 1024 && bufferSize <= 2048){
                        bufferSize = 2048;
                    }else if (bufferSize > 2048 && bufferSize <= 4096){
                        bufferSize = 4096;
                    }else if (bufferSize > 4096 && bufferSize <= 8192){
                        bufferSize = 8192;
                    }else if (bufferSize > 8192 && bufferSize <= 16384){
                        bufferSize = 16384;
                    }else{
                        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) {
                            Log.d("found", "rate: " + rate + " channelConfig: " + channelConfig + " bufferSize: " + bufferSize + " audioFormat: " + audioFormat);
                            sampleRate = rate;
                            channelConfiguration = channelConfig;
                            audioEncoding = audioFormat;
                            buffersizebytes = bufferSize;
                            return recorder;
                        }
                    }
                } catch (Exception e) {
                    Log.d("audioSetup", rate + "Exception, keep trying.", e);
                    e.printStackTrace();
                }
            }
        }
    }
    return null;
}

似乎很好用。谢谢你的帮助。