I am trying to instantiate an AudioRecord
in order to monitor the audio in real time, but it fails even though I try multiple different configurations.
I've tried restarting my phone multiple times, and I have
<uses-permission android:name="android.permission.RECORD_AUDIO" />
in my manifest (in the <manifest>
tag, not in the <application>
tag).
The solutions I've found for this problem have been to make sure the permission is correct and is in the right place (check), to make sure to call release()
on all AudioRecord
instances (check), to restart the phone (check) and to use a method of finding a valid combination of sample rate, audio format and channel configuration (check).
I've tried this on a Samsung Galaxy S5 running Android 4.4.2 and a Motorola Moto G running Android 4.4.4 by the way.
Here's my code for trying to find a valid AudioRecord
:
public AudioRecord findAudioRecord() {
int[] mSampleRates = new int[] { 44100, 11025, 22050, 16000, 8000 };
short [] aformats = new short[] { AudioFormat.ENCODING_PCM_16BIT, AudioFormat.ENCODING_PCM_8BIT };
short [] chConfigs = new short[] { AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO };
for (int rate : mSampleRates) {
for (short audioFormat : aformats) {
for (short channelConfig : chConfigs) {
try {
Log.d("SoundMeter", "Attempting rate " + rate + "Hz, bits: " + audioFormat + ", channel: " + channelConfig);
int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);
if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {
Log.d("SoundMeter", "Buffer size OK");
// Try to instantiate
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 ){
Log.d("SoundMeter", "Success: AudioRecord initialized!");
return recorder;
}
else {
Log.d("SoundMeter", "Failed: AudioRecord not initialized");
// Important! Release recorder
recorder.release();
}
}
}
catch (Exception e) {
Log.e("SoundMeter", "Exception on sample rate " + rate + "\n ", e);
}
}
}
}
Log.println(Log.ASSERT, "SoundMeter", "No valid audio record configuration found!");
return null;
}
And here's the logcat from running the above method:
05-20 11:42:34.146 8326-8326/com.my.app D/SoundMeter: Attempting rate 44100Hz, bits: 2, channel: 16
05-20 11:42:34.156 8326-8326/com.my.app D/SoundMeter: Buffer size OK
05-20 11:42:34.196 8326-8326/com.my.app E/AudioRecord: AudioFlinger could not create record track, status: -12
05-20 11:42:34.196 8326-8326/com.my.app E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed.
05-20 11:42:34.196 8326-8326/com.my.app E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
05-20 11:42:34.196 8326-8326/com.my.app D/SoundMeter: Failed: AudioRecord not initialized
05-20 11:42:34.196 8326-8326/com.my.app D/SoundMeter: Attempting rate 44100Hz, bits: 2, channel: 12
05-20 11:42:34.196 8326-8326/com.my.app D/SoundMeter: Buffer size OK
05-20 11:42:34.196 8326-8326/com.my.app E/AudioRecord: AudioFlinger could not create record track, status: -12
05-20 11:42:34.206 8326-8326/com.my.app E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed.
05-20 11:42:34.206 8326-8326/com.my.app E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
05-20 11:42:34.206 8326-8326/com.my.app D/SoundMeter: Failed: AudioRecord not initialized
05-20 11:42:34.206 8326-8326/com.my.app D/SoundMeter: Attempting rate 44100Hz, bits: 3, channel: 16
05-20 11:42:34.206 8326-8326/com.my.app E/android.media.AudioRecord: getMinBufferSize(): Invalid audio format.
05-20 11:42:34.206 8326-8326/com.my.app D/SoundMeter: Attempting rate 44100Hz, bits: 3, channel: 12
05-20 11:42:34.206 8326-8326/com.my.app E/android.media.AudioRecord: getMinBufferSize(): Invalid audio format.
05-20 11:42:34.206 8326-8326/com.my.app D/SoundMeter: Attempting rate 11025Hz, bits: 2, channel: 16
05-20 11:42:34.206 8326-8326/com.my.app D/SoundMeter: Buffer size OK
05-20 11:42:34.206 8326-8326/com.my.app E/AudioRecord: AudioFlinger could not create record track, status: -12
05-20 11:42:34.206 8326-8326/com.my.app E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed.
05-20 11:42:34.206 8326-8326/com.my.app E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
05-20 11:42:34.206 8326-8326/com.my.app D/SoundMeter: Failed: AudioRecord not initialized
05-20 11:42:34.206 8326-8326/com.my.app D/SoundMeter: Attempting rate 11025Hz, bits: 2, channel: 12
05-20 11:42:34.206 8326-8326/com.my.app D/SoundMeter: Buffer size OK
05-20 11:42:34.216 8326-8326/com.my.app E/AudioRecord: AudioFlinger could not create record track, status: -12
05-20 11:42:34.216 8326-8326/com.my.app E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed.
05-20 11:42:34.216 8326-8326/com.my.app E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
05-20 11:42:34.216 8326-8326/com.my.app D/SoundMeter: Failed: AudioRecord not initialized
05-20 11:42:34.216 8326-8326/com.my.app D/SoundMeter: Attempting rate 11025Hz, bits: 3, channel: 16
05-20 11:42:34.216 8326-8326/com.my.app E/android.media.AudioRecord: getMinBufferSize(): Invalid audio format.
05-20 11:42:34.216 8326-8326/com.my.app D/SoundMeter: Attempting rate 11025Hz, bits: 3, channel: 12
05-20 11:42:34.216 8326-8326/com.my.app E/android.media.AudioRecord: getMinBufferSize(): Invalid audio format.
05-20 11:42:34.216 8326-8326/com.my.app D/SoundMeter: Attempting rate 22050Hz, bits: 2, channel: 16
05-20 11:42:34.216 8326-8326/com.my.app D/SoundMeter: Buffer size OK
05-20 11:42:34.216 8326-8326/com.my.app E/AudioRecord: AudioFlinger could not create record track, status: -12
05-20 11:42:34.216 8326-8326/com.my.app E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed.
05-20 11:42:34.216 8326-8326/com.my.app E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
05-20 11:42:34.216 8326-8326/com.my.app D/SoundMeter: Failed: AudioRecord not initialized
05-20 11:42:34.216 8326-8326/com.my.app D/SoundMeter: Attempting rate 22050Hz, bits: 2, channel: 12
05-20 11:42:34.226 8326-8326/com.my.app D/SoundMeter: Buffer size OK
05-20 11:42:34.226 8326-8326/com.my.app E/AudioRecord: AudioFlinger could not create record track, status: -12
05-20 11:42:34.226 8326-8326/com.my.app E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed.
05-20 11:42:34.226 8326-8326/com.my.app E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
05-20 11:42:34.226 8326-8326/com.my.app D/SoundMeter: Failed: AudioRecord not initialized
05-20 11:42:34.226 8326-8326/com.my.app D/SoundMeter: Attempting rate 22050Hz, bits: 3, channel: 16
05-20 11:42:34.226 8326-8326/com.my.app E/android.media.AudioRecord: getMinBufferSize(): Invalid audio format.
05-20 11:42:34.226 8326-8326/com.my.app D/SoundMeter: Attempting rate 22050Hz, bits: 3, channel: 12
05-20 11:42:34.226 8326-8326/com.my.app E/android.media.AudioRecord: getMinBufferSize(): Invalid audio format.
05-20 11:42:34.226 8326-8326/com.my.app D/SoundMeter: Attempting rate 16000Hz, bits: 2, channel: 16
05-20 11:42:34.226 8326-8326/com.my.app D/SoundMeter: Buffer size OK
05-20 11:42:34.226 8326-8326/com.my.app E/AudioRecord: AudioFlinger could not create record track, status: -12
05-20 11:42:34.236 8326-8326/com.my.app E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed.
05-20 11:42:34.236 8326-8326/com.my.app E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
05-20 11:42:34.236 8326-8326/com.my.app D/SoundMeter: Failed: AudioRecord not initialized
05-20 11:42:34.236 8326-8326/com.my.app D/SoundMeter: Attempting rate 16000Hz, bits: 2, channel: 12
05-20 11:42:34.236 8326-8326/com.my.app D/SoundMeter: Buffer size OK
05-20 11:42:34.236 8326-8326/com.my.app E/AudioRecord: AudioFlinger could not create record track, status: -12
05-20 11:42:34.236 8326-8326/com.my.app E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed.
05-20 11:42:34.236 8326-8326/com.my.app E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
05-20 11:42:34.236 8326-8326/com.my.app D/SoundMeter: Failed: AudioRecord not initialized
05-20 11:42:34.236 8326-8326/com.my.app D/SoundMeter: Attempting rate 16000Hz, bits: 3, channel: 16
05-20 11:42:34.236 8326-8326/com.my.app E/android.media.AudioRecord: getMinBufferSize(): Invalid audio format.
05-20 11:42:34.236 8326-8326/com.my.app D/SoundMeter: Attempting rate 16000Hz, bits: 3, channel: 12
05-20 11:42:34.236 8326-8326/com.my.app E/android.media.AudioRecord: getMinBufferSize(): Invalid audio format.
05-20 11:42:34.236 8326-8326/com.my.app D/SoundMeter: Attempting rate 8000Hz, bits: 2, channel: 16
05-20 11:42:34.236 8326-8326/com.my.app D/SoundMeter: Buffer size OK
05-20 11:42:34.246 8326-8326/com.my.app E/AudioRecord: AudioFlinger could not create record track, status: -12
05-20 11:42:34.246 8326-8326/com.my.app E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed.
05-20 11:42:34.246 8326-8326/com.my.app E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
05-20 11:42:34.246 8326-8326/com.my.app D/SoundMeter: Failed: AudioRecord not initialized
05-20 11:42:34.246 8326-8326/com.my.app D/SoundMeter: Attempting rate 8000Hz, bits: 2, channel: 12
05-20 11:42:34.246 8326-8326/com.my.app D/SoundMeter: Buffer size OK
05-20 11:42:34.246 8326-8326/com.my.app E/AudioRecord: AudioFlinger could not create record track, status: -12
05-20 11:42:34.256 8326-8326/com.my.app E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed.
05-20 11:42:34.256 8326-8326/com.my.app E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
05-20 11:42:34.256 8326-8326/com.my.app D/SoundMeter: Failed: AudioRecord not initialized
05-20 11:42:34.256 8326-8326/com.my.app D/SoundMeter: Attempting rate 8000Hz, bits: 3, channel: 16
05-20 11:42:34.256 8326-8326/com.my.app E/android.media.AudioRecord: getMinBufferSize(): Invalid audio format.
05-20 11:42:34.256 8326-8326/com.my.app D/SoundMeter: Attempting rate 8000Hz, bits: 3, channel: 12
05-20 11:42:34.256 8326-8326/com.my.app E/android.media.AudioRecord: getMinBufferSize(): Invalid audio format.
05-20 11:42:34.256 8326-8326/com.my.app A/SoundMeter: No valid audio record configuration found!