16

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!

slezadav
  • 5,936
  • 6
  • 33
  • 61
Gene
  • 173
  • 1
  • 1
  • 8

7 Answers7

16

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.

Liudvikas Bukys
  • 5,410
  • 3
  • 23
  • 36
9

Had the same error, till I restarted the device.

It seems that on my Galaxy S the native impl is buggy: several times acquiring and releasing an AudioRecorder (during whole phone uptime) causes that error.

java.is.for.desktop
  • 9,703
  • 10
  • 62
  • 100
5

After using audio recorder, you must stop and release it. Then when you init the audio recorder the next time, it's ok.

user
  • 85,380
  • 17
  • 189
  • 186
Wills
  • 51
  • 1
  • 1
4

Hi I've had the very same issue while trying to initialize an AudioRecord object and the solution I've found was to test the configurtion parameter before actually trying to instantiate the current AudioRecord object. Here is the rouine I've used:

    /**
 * Scan for the best configuration parameter for AudioRecord object on this device.
 * Constants value are the audio source, the encoding and the number of channels.
 * That means were are actually looking for the fitting sample rate and the minimum
 * buffer size. Once both values have been determined, the corresponding program
 * variable are initialized (audioSource, sampleRate, channelConfig, audioFormat)
 * For each tested sample rate we request the minimum allowed buffer size. Testing the
 * return value let us know if the configuration parameter are good to go on this
 * device or not.
 * 
 * This should be called in at start of the application in onCreate().
 * 
 * */
public void initRecorderParameters(int[] sampleRates){

    for (int i = 0; i < sampleRates.length; ++i){
        try {
            Log.i(TAG, "Indexing "+sampleRates[i]+"Hz Sample Rate");
            int tmpBufferSize = AudioRecord.getMinBufferSize(sampleRates[i], 
                            AudioFormat.CHANNEL_IN_MONO,
                            AudioFormat.ENCODING_PCM_16BIT);

            // Test the minimum allowed buffer size with this configuration on this device.
            if(tmpBufferSize != AudioRecord.ERROR_BAD_VALUE){
                // Seems like we have ourself the optimum AudioRecord parameter for this device.
                AudioRecord tmpRecoder = new AudioRecord(MediaRecorder.AudioSource.MIC, 
                                                        sampleRates[i], 
                                                        AudioFormat.CHANNEL_IN_MONO,
                                                        AudioFormat.ENCODING_PCM_16BIT,
                                                        tmpBufferSize);
                // Test if an AudioRecord instance can be initialized with the given parameters.
                if(tmpRecoder.getState() == AudioRecord.STATE_INITIALIZED){
                    String configResume = "initRecorderParameters(sRates) has found recorder settings supported by the device:"  
                                        + "\nSource   = MICROPHONE"
                                        + "\nsRate    = "+sampleRates[i]+"Hz"
                                        + "\nChannel  = MONO"
                                        + "\nEncoding = 16BIT";
                    Log.i(TAG, configResume);

                    //+++Release temporary recorder resources and leave.
                    tmpRecoder.release();
                    tmpRecoder = null;

                    return;
                }                   
            }else{
                Log.w(TAG, "Incorrect buffer size. Continue sweeping Sampling Rate...");
            }
        } catch (IllegalArgumentException e) {
            Log.e(TAG, "The "+sampleRates[i]+"Hz Sampling Rate is not supported on this device");
        }
    }
}

I Hop this help.

Dickwan

dickwan
  • 317
  • 2
  • 12
3

If you did not take Record_Audio permission then take it even than you getting error then switch off your mobile than switch on again then run your app.

Singhak
  • 7,218
  • 2
  • 27
  • 32
3

This is also affecting Sansung Galaxy tabs GT-P1000 and later 10.1 one

This bug is pretty hard to reproduce and a reboot is the only way I found to escape this bad state ...

Is there a bug tracker to follow at samsung ?

RzR
  • 2,889
  • 26
  • 25
0

I think the audio hardware cannot support up to 10 channels, which you set as input, so try to use 2 channels, and to see whether it can work.

thisEric
  • 306
  • 6
  • 17