AudioRecord start() error status -38

不想你离开。 提交于 2019-12-02 01:03:54
mkrinblk

alright so this is what i think i needed to do this should really be talked about in the AudioRecorder documentation but this similar questioin led me to the answer AudioRecord object not initializing basically what you want to do is loop through all the configurations and try each against the AudioRecord.ERROR_BAD_VALUE check also since I was planning on using a fft in which the length must be a power of 2 i added a little if else if portion if anybody else runs into a similar situation

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

seems to work nicely. thanks for the help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!