Reputation: 71
I have checked and tried all the other threads for multiple hours today and none of the solutions work.
I have tried filtering through all avaible audio options. I have given the proper permissions to the app.
Goal: I am trying to get this audio stream so I can get the frequency of the audio.
My stuff
public int audioSource = MediaRecorder.AudioSource.MIC;
public int channelConfig = AudioFormat.CHANNEL_IN_MONO;
public int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
public AudioRecord audioRecord = null;
private Thread recordingThread = null;
public int blockSize = 256; // deal with this many samples at a time
public int sampleRate = 8000; // Sample rate in Hz
later on...
int bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioEncoding);
AudioRecord audioeeeeRecord = new AudioRecord(audioSource, sampleRate, channelConfig, audioEncoding, bufferSize); // The RAW PCM sample recording
audioRecord = audioeeeeRecord;
if (audioRecord != null && audioRecord.getState() != AudioRecord.STATE_INITIALIZED)
try {
throw new Exception("AudioRecord init failed"); //audioRecord = findAudioRecord();
} catch (Exception e) {
e.printStackTrace();
}
final short[] buffer = new short[blockSize];
try {
audioRecord.startRecording();
} catch (Exception e) {
e.printStackTrace();
}
This throws the following error (http://pastebin.com/raw/3wmA4cku):
AudioRecord: AudioFlinger could not create record track, status: -1
E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1.
E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
W/System.err: java.lang.Exception: AudioRecord init failed
W/System.err: at app.mobile.mobileecg.MainActivity.startReading(MainActivity.java:102)
W/System.err: at app.mobile.mobileecg.MainActivity$1.onClick(MainActivity.java:58)
W/System.err: at android.view.View.performClick(View.java:5697)
W/System.err: at android.view.View$PerformClick.run(View.java:22526)
W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:158)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7229)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
W/System.err: java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
W/System.err: at android.media.AudioRecord.startRecording(AudioRecord.java:943)
W/System.err: at app.mobile.mobileecg.MainActivity.startReading(MainActivity.java:114)
W/System.err: at app.mobile.mobileecg.MainActivity$1.onClick(MainActivity.java:58)
W/System.err: at android.view.View.performClick(View.java:5697)
W/System.err: at android.view.View$PerformClick.run(View.java:22526)
W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:158)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7229)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Upvotes: 3
Views: 6846
Reputation: 8905
Step 1) Check your manifest to ensure add RECORD_AUDIO permission under application section. e.g.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.colibri.audioloopback">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
</manifest>
Step 2) If your device is M or later, you need to grant the permssion from Settings->App->Your App->Permissions.
Step 3) Refer to below code snippet if still doesn't work.
public void audioRecordLoop() throws Exception {
Log.e(TAG,"start audioRecordLoop");
int channelConfig = mChannels == 2?
AudioFormat.CHANNEL_IN_STEREO:
AudioFormat.CHANNEL_IN_MONO;
int bufferSize = AudioRecord.getMinBufferSize(
mSampleRateHz, channelConfig, mAudioEncoding);
mAudioRecord = new AudioRecord(
mAudioSource, mSampleRateHz, channelConfig, mAudioEncoding, bufferSize);// The RAW PCM sample recording
if (mAudioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
Log.e(TAG,"AudioRecord init failed");
return;
}
final short[] buffer = new short[mBlockSize];
mAudioRecord.startRecording();
int len = 0;
while (mbExit == false) {
len = mAudioRecord.read(buffer, 0, mBlockSize);
if (len < 0) {
Log.e(TAG,"read error " + len);
return;
}
}
mAudioRecord.stop();
mAudioRecord.release();
}
It should work now!
Upvotes: 6
Reputation: 71
This is the answer. It is stupid that this isn't something that android has.
https://stackoverflow.com/a/33769527/6530367
I know this is a very old question but in Android Marshmallow you have to go on "Settings > Apps > Your App > Permissions" and enble Microphone permission.
Upvotes: 1
Reputation: 2520
I think the problem may be that you had tied up the audio resources with a previous attempt, then got the code correct and now it doesn't work because there are no audio resources available. See this SO question. Also, you are throwing an exception when the constructor returns an uninitialized AudioRecord, but you catch your own exception and this results in a second exception (... called on uninitialized AudioRecord ...) in the log that can be ignored as it is a result of the first.
You might need to put some try/finally sections and/or an OnDestroy and or OnPause method in your activity to ensure that the audioRecord gets released when no longer used.
Once you have the cleanup code in place, you may need to kill off your app and/or reboot your phone to free the audio resources.
Of course it is possible that you left another app running that is holding the microphone but I doubt it.
Upvotes: 2