Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Could not get audio input for record source 1" when trying to initialize AudioRecord

Tags:

android

I keep getting this error when trying to initialize and AudioRecord object, in an attempt to record sounds using the emulator using Eclipse.

I have tried with various bit sampling rates, 8000 is the only one that is valid, but the error continues to appear. I have tried on various versions of the sdk, 1.5, 1.6, 2, 2.2 and 2.3.1. (and combinations of with the AVD).

Here is the code:

       Log.v(TAG, "About to initialize recording");
   //int[] samplingRates = {44100, 22050, 16000, 11025, 8000};
   int[] samplingRates = {8000};

    for (int i = 0; i < samplingRates.length; ++i)
    {
        try
        {
            Log.d(TAG,"Trying sampling rate: " + samplingRates[i]);
            int min = AudioRecord.getMinBufferSize(samplingRates[i], 
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    //AudioFormat.CHANNEL_IN_MONO,
                    AudioFormat.ENCODING_PCM_16BIT);
            Log.d(TAG,"MinBufferSize: " + min);

            AudioRecord record = new AudioRecord(MediaRecorder.AudioSource.MIC, samplingRates[i],
                    AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, min);
            if (record.getState() == AudioRecord.STATE_INITIALIZED)
            {
                Log.d("Recorder", "Audio recorder initialised at " + record.getSampleRate());
                return record;
            }
            record.release();
            record = null;
        }
        catch (IllegalArgumentException e)
        {
            // Try the next one.
            Log.d(TAG,"Initialization failed");
        }
    }
    // None worked.
    return null;

The error occurs on instantiation, AudioRecord record = new etc.

Has anyone seen this same problem, it seems to be a straight forward request so i´m suprised if this a bug, particular as I can´t get it working with different versions.

I have created the AVDs with Audio Record capabilities. Is there anything else I have to configure?

I have seen a couple of other posts on the same issue, but no replies.

Thanks in advance

Chris

like image 922
Chris Avatar asked Feb 10 '11 20:02

Chris


1 Answers

You problem here is probably actually twofold. First, you have to get all of the parameters just right for the AudioRecord, ie, I can see that you tried all the various sampling rates and channel configurations, but did you try different bit depths? If you don't get it all just right, it doesn't work. Furthermore, it would appear that some android devices seem to report the wrong minBufferSize.

But, I think the actual problem you are experiencing is that the AVDs cannot record Audio/Video (even though they may not complain if you configure it so): http://developer.android.com/guide/topics/media/index.html

From paragraph three:

Note that the emulator doesn't have hardware to capture audio or video, but actual mobile devices are likely to provide these capabilities, accessible through the MediaRecorder class.

like image 75
J_Y_C Avatar answered Oct 19 '22 23:10

J_Y_C