0

When I am trying to record an audio through the emulator using following code

  AudioRecord recordInstance = new AudioRecord(
    MediaRecorder.AudioSource.MIC, this.getFrequency(), this
      .getChannelConfiguration(), this.getAudioEncoding(),
    bufferSize);

Then I am getting following exceptions in logcat:

12-16 19:07:31.680: INFO/jdwp(223): Ignoring second debugger -- accepting and dropping
12-16 19:07:31.700: ERROR/AudioHardware(34): Error opening input channel
12-16 19:07:31.720: WARN/AudioHardwareInterface(34): getInputBufferSize bad sampling rate: 11025
12-16 19:07:31.730: ERROR/AudioRecord(294): Recording parameters are not supported: sampleRate 11025, channelCount 1, format 1
12-16 19:07:31.730: ERROR/AudioRecord-JNI(294): Error creating AudioRecord instance: initialization check failed.

12-16 19:07:31.730: ERROR/AudioRecord-Java(294): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object.
12-16 19:07:31.730: WARN/dalvikvm(294): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
12-16 19:07:31.770: ERROR/AndroidRuntime(294): FATAL EXCEPTION: Thread-8
12-16 19:07:31.770: ERROR/AndroidRuntime(294): java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
12-16 19:07:31.770: ERROR/AndroidRuntime(294):     at android.media.AudioRecord.startRecording(AudioRecord.java:495)
12-16 19:07:31.770: ERROR/AndroidRuntime(294):     at com.prospeak.Recorder.run(Recorder.java:84)
12-16 19:07:31.770: ERROR/AndroidRuntime(294):     at java.lang.Thread.run(Thread.java:1096)

Can you figure out what's wrong in this code?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Parvendra
  • 141
  • 1
  • 2
  • 7

7 Answers7

3

your sample rate is wrong, try 8000Hz. It is an emulator limitation.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • Thank you very much Vladimir and Christian. Its working. But when I am recording audio then there is not voice when I play it back. Also, how to capture the decibel value of the voice at a particular time of period. Basically I want to capture some 3-5 seconds voice and want to know the high and low loudness of the voice. Please suggest the way to achieve it. thanks Parvendra – Parvendra Dec 20 '10 at 05:05
2

Also, make sure that you have this permission set in your AndroidManifest.xml: <uses-permission android:name="android.permission.RECORD_AUDIO" />

Michael Litvin
  • 3,976
  • 1
  • 34
  • 40
1

If you haven't registered the permission to record audio in your manifest then you will get an error.

Christian
  • 25,249
  • 40
  • 134
  • 225
  • That was the issue in my case! See: http://stackoverflow.com/questions/4843739/audiorecord-object-not-initializing – Sney Apr 16 '13 at 14:16
0

I struggled with a similar issue recently. As said earlier you have to cycle through all possible combinations of Sample rates, Channel configurations, Audio Formats to know what fits for a given phone. The emulator AFAIK, does not support audio input and will always give an initialization error on AudioRecord objects. So you have to test your code on a real device. But additionally, I discovered that there is a maximum limit to the buffer size you can give for the AudioRecord object. It must be somewhere in a few KB's. I tried to set it at about 10 MB and it kept giving the same initialization error. So be careful. Here is the code to cycle through the Formats and Sample rates(courtesy somewhere on StackEx):

 private static int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 };
private static short [] aformats = new short[] { AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT };
private static short [] chConfigs = new short[] { AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO };


public AudioRecord findAudioRecord() {
    for (int rate : mSampleRates) {
        for (short audioFormat : aformats) {
            for (short channelConfig : chConfigs) {
                try {
                    Log.d("Log:", "Attempting rate " + rate + "Hz, bits: " + audioFormat + ", channel: "
                            + channelConfig);
                    int 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(android.media.MediaRecorder.AudioSource.MIC, rate, channelConfig, audioFormat, java.lang.Math.max(bufferSize,1024*800));

                        if (recorder.getState() == AudioRecord.STATE_INITIALIZED){
                            Chosen_SR = rate;
                            audFormat = audioFormat;
                            chanConfig = channelConfig;
                            return recorder;
                        }

                    }
                } catch (Exception e) {
                    Log.e("Log:", rate + "Exception, keep trying.",e);
                }
            }
        }
    }
    return null;
}
AudioRecorder mRecord = findAudioRecord();

Hope that helps.

shaktiman
  • 1
  • 1
0

The error is:

getInputBufferSize bad sampling rate: 11025

You need a different sample rate. You could loop through potential sample rates until you don't have an Exception.

As far as trying to record from the Emulator, it says on the Android Developer site that it's not possible so it would probably be best to test from a phone or to use a pre-recorded sample.

chaimp
  • 16,897
  • 16
  • 53
  • 86
0

Make sure that you check that the audiorecord.getState() == initialized.

Sample code in that linked post:

https://stackoverflow.com/questions/tagged/sample-rate+android

Community
  • 1
  • 1
RzR
  • 3,068
  • 29
  • 26
0

guys audio record only work on,

    AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
            8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT, 500000);
    recorder.startRecording();

i search it for long time and last found that, but it only produces noise, try to get good quality audio