0

I want to capture the audio so I am using the AudioRecord. But I'm getting null in the AudioRecord. Please help me so that it work in all the phones.

private static int[] mSampleRates = new int[] { 44100, 44056, 47250, 48000, 22050, 16000, 11025, 8000 };


public AudioRecord findAudioRecord() {
    for (int rate : mSampleRates) {
        for (short audioFormat : new short[] { AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT }) {
            for (short channelConfig : new short[] { AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO }) {
                try {
                    Log.d(LOG_TAG, "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(AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize);

                        if (recorder.getState() == AudioRecord.STATE_INITIALIZED)
                            return recorder;
                    }
                } catch (Exception e) {
                    Log.e(LOG_TAG, rate + "Exception, keep trying.",e);
                }
            }
        }
    }
    return null;
}

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
android
  • 3,261
  • 3
  • 16
  • 18
  • Try some other AudioSources, like `MIC`. – Michael Nov 23 '13 at 21:24
  • I tried like MIC but still null.. I tried with this `AudioRecord recorder = new AudioRecord(AudioSource.MIC, rate, channelConfig, audioFormat, bufferSize);` This condition false `if (recorder.getState() == AudioRecord.STATE_INITIALIZED)` – android Nov 23 '13 at 21:35

1 Answers1

0

do you have

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

in your manifest?

dm78
  • 1,570
  • 1
  • 16
  • 28