其他分享
首页 > 其他分享> > android-AudioRecord start()错误状态-38

android-AudioRecord start()错误状态-38

作者:互联网

我正在尝试设置一个录音机,但不断出现特定错误,我不知道为什么.在我的代码中,我在startrecording()方法之前和之后通过Log检查了录音机处于什么状态.

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()函数,因此我认为这不是问题所在.

任何帮助是极大的赞赏.

解决方法:

好吧,这就是我认为我需要做的,应该在AudioRecorder文档中进行讨论,但是这个类似的问题使我得到了答案AudioRecord object not initializing
基本上,您要做的是遍历所有配置,并针对AudioRecord.ERROR_BAD_VALUE进行检查,这也是因为我计划使用长度必须为2的幂的fft,所以我添加了一点if if if if if anybody否则会遇到类似情况

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

似乎效果很好.谢谢您的帮助.

标签:android,android-audiorecord
来源: https://codeday.me/bug/20191011/1892060.html