Reputation: 211
public class AudioRecorderActivity extends Activity {
private static final int RECORDER_SAMPLERATE = 8000;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private AudioRecord recorder = null;
private static final String TAG = "AudioRecorderActivity";
short[][] buffers = new short[256][160];
int ix = 0;
private boolean stopped = false;
private void startRecording() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
try {
int N = AudioRecord.getMinBufferSize (
RECORDER_SAMPLERATE,
RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING) * 20;
recorder = new AudioRecord(AudioSource.MIC,
RECORDER_SAMPLERATE,
RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING,N );
recorder.startRecording();
while(!stopped) {
short[] buffer = buffers[ix++ % buffers.length];
N = recorder.read(buffer,0,buffer.length);
}
}
catch(Throwable x) {
Log.v(TAG,"Error reading voice audio",x);
x.printStackTrace();
}
finally {
stopped = true;
stopRecording();
}
}
}
Question : Though the code snippet is based on an exaple from StackOverflow, it is not working Please let me know what could be the mistake ?
12-20 03:44:32.271: E/AudioRecord(224): AudioFlinger could not create record track, status: -1
12-20 03:44:32.271: E/AudioRecord-JNI(224): Error creating AudioRecord instance: initialization check failed.
12-20 03:44:32.271: E/AudioRecord-Java(224): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object.
Upvotes: 21
Views: 15706
Reputation: 31
Giving Explicit Permission fixed the issue for me.
Try these steps
Android Version 7.0
Settings -> Applications -> <MyAPP> -> Permissions
-> [Turn Camera and Microphone ON]
Upvotes: 1
Reputation: 259
The solution for API 23 (Marshmallow) is simple. You must set "targetSdkVersion 22" in the bulid.gradle file, after get bellow permission.
Upvotes: 2
Reputation: 2515
Solution for Pre-Marshmallow versions of android will be same as Olexij mentioned...
<uses-permission android:name="android.permission.RECORD_AUDIO" />
However as starting Android API 23 (Marshmallow) this is considered and dangerous permission and you have to request for this permission from within the Activity where it is being used. Dave's experience is result of that; if you declare permission in Manifest but do not request at runtime (atleast first time) then it will show in App Permissions in Application Manager but will stay turned off.
Check...
Upvotes: 5
Reputation: 310
On recent Android versions, it seems that repushing the apk from Android Studio after changing the permissions in the manifest doesn't actually change the permission in the app settings. This is why the above answer worked for me. I had built an application without the RECORD_AUDIO permission in my manifest, and seen the permission error in my log (along with the errors in the OP). I added the permission to my manifest and reran the application on device, the permission error in the log was no longer there but I still got the errors in the OP. I went to my System, Applications, Application Manager -> MyApp -> Permissions, and saw that Record Audio was listed but still turned off. Manually turning that on fixed it.
I imagine (but haven't confirmed) that if I built the correct permission into my manifest in the first place, this would have just worked for me. Alternatively, uninstalling my app from the device, and then reinstalling, might fix too, but I haven't tried that either.
Upvotes: 16
Reputation: 571
Add record_audio permission to AndroidManifest.xml
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Upvotes: 45