<AudioRecord> "Could not get audio input for record source 1"

Gene picture Gene · Jan 26, 2011 · Viewed 20.4k times · Source

I've been having this issue with initializing AudioRecord for Android. I searched for quite a while on the web with no success.

For the phone, I'm using a Samsung GalaxyS on SDK version 7. For the AudioRecord initialization, I'm using 8000 as the sample rate, MONO for channel config, 16bit for audio format, and according to the log, the minBufferSize is set to be 4160. I have added the AUDIO_RECORD permission to the manifest.

My code for the initialization is as follows:

...
private static int SAMPLE_RATE = 8000;
private static int CHANNEL_CONFIG = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private static int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
// ??? Both 8Bit and Default are deemed illegal.

public MicVolumeManager() {
    this.bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE,
        CHANNEL_CONFIG, AUDIO_FORMAT);
    PhoneDebugger.debug("AUDIO-BUFFER-SIZE", 
        Integer.toString(this.bufferSize));

    this.recorder = new AudioRecord(AudioSource.MIC, SAMPLE_RATE,
        CHANNEL_CONFIG, AUDIO_FORMAT, this.bufferSize);

    this.audioBuffer = new byte[this.bufferSize];
}
...

However, the object (this.recorder) failed to be initialized. The following is from the log using DDMS:

AUDIO-BUFFER-SIZE(3253): 4160
AudioRecord(3253): set(): sampleRate 8000, channels 16, frameCount 2080
AudioPolicyManager(2175): getInput() inputSource 1, samplingRate 8000, format 1, channels 10, acoustics 0
AudioFlinger(2175): openInput() openInputStream returned input 0x0, SamplingRate 8000, Format 1, Channels 10, acoustics 0, status -17
AudioRecord(3253): Could not get audio input for record source 1
AudioRecord-JNI(3253): Error creating AudioRecord instance: initialization check failed.
AudioRecord-Java(3253): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object.

Any help please? Many thanks!

Answer

Liudvikas Bukys picture Liudvikas Bukys · Jul 14, 2011

For me, the cause was failure to call AudioRecord.release() for a previous instance of AudioRecord; it tied up native resources in AudioFlinger and interfered with subsequent AudioRecord instances. Saw it on a Samsung Fascinate (Galaxy S) Android 2.1 (Eclair); either the Eclair or the Samsung implementation may be particularly intolerant.