问题
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 ?
Here is the Error Message
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.
回答1:
Add record_audio permission to AndroidManifest.xml
<uses-permission android:name="android.permission.RECORD_AUDIO" />
回答2:
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.
回答3:
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...
- Requesting Permissions at Run Time
- ActivityCompat | Android Developers
回答4:
The solution for API 23 (Marshmallow) is simple. You must set "targetSdkVersion 22" in the bulid.gradle file, after get bellow permission.
回答5:
Giving Explicit Permission fixed the issue for me.
Try these steps
Android Version 7.0
Settings -> Applications -> <MyAPP> -> Permissions
-> [Turn Camera and Microphone ON]
来源:https://stackoverflow.com/questions/20693001/audioflinger-could-not-create-record-track-status-1-need-help-to-ifx