Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AudioRecord initialization fails every time

My problem is simple to explain -- I am trying to create a AudioRecord object but it fails to initialize (ie after the constructor, getState returns 0, indicating failure). I am running this from Eclipse on a MotoDroid 1 running OS 2.2.1. My AndroidManifest.xml is, AFAIK, using the right permission, RECORD_AUDIO (I don't know how to confirm this):

<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <activity android:name=".SphinxMic"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

I do the following to create the AudioRecord:

bufferSize = AudioRecord.getMinBufferSize(8000, CHANNEL_IN_MONO, ENCODING_PCM_8BIT);
audioRecorder = new AudioRecord(AudioSource.MIC, 8000, CHANNEL_IN_MONO, ENCODING_PCM_8BIT, 50*bufferSize);
if (audioRecorder.getState() != AudioRecord.STATE_INITIALIZED)
  throw new Exception("AudioRecord init failed");

audioRecorder.getState() returns 0 (ie STATE_UNINITIALIZED)

I have not found any complete examples of using this API and I'm very much an Android beginner so the solution may well be something simple. What can I do to find out why it fails?

A few people have asked similar questions, but they must have been having different problems than me because the fixes they approve haven't helped me. Most notably this. But the approved solution is perplexing and didn't work for me anyway. I have also tried a variety of bit rates (8000, 16000, 11025, 44100), both mono and stereo and 8 and 16 bit. No combination comes back as successfully initialized.

like image 448
Dave Avatar asked Dec 19 '10 10:12

Dave


3 Answers

I spent some hours solving this problem, and found out that moving

<uses-permission android:name="android.permission.RECORD_AUDIO" />

outside the application block actually solved it!

...
    </application>

    <uses-permission android:name="android.permission.RECORD_AUDIO" /> 
</manifest>
like image 81
Lukas Batteau Avatar answered Oct 22 '22 11:10

Lukas Batteau


Another possible issue:

I had the RECORD_AUDIO permission enabled in my manifest, but for API level 23 I had to also request the permission at runtime.

like image 24
brainmurphy1 Avatar answered Oct 22 '22 11:10

brainmurphy1


Try 16bit. This is what works for me:

try {
        // Create a new AudioRecord object to record the audio.
        bufferSize = AudioRecord.getMinBufferSize(frequency,channelConfiguration,audioEncoding);

        audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, 
                                                  frequency, channelConfiguration, 
                                                  audioEncoding, bufferSize);
    } catch (Throwable t) {
        Log.e("AudioRecord","Recording Failed");
    }

And I have the following variables set:

int frequency = 8000;
int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
like image 1
chaimp Avatar answered Oct 22 '22 10:10

chaimp