Showing posts with label audio-recording. Show all posts
Showing posts with label audio-recording. Show all posts

Thursday, June 29, 2017

Android microphone constantly gives 32639 or -32640 on newer devices

Leave a Comment

I've implemented code similar to this. I have a noise alert go off in the Log, but it always gives 32639 or -32640 regardless of what kind of noise is going on outside.

short[] buffer = new short[minSize]; boolean thresholdMet = false; int threshold = sliderThreshold.getProgress();  ar.read(buffer, 0, minSize);  //Iterate through each chunk of amplitude data //Check if amplitude is greater than threshold for (short s : buffer) {     if (Math.abs(s) > threshold) {         thresholdMet = true;         Log.w("NoiseThreshold", String.valueOf(s));         break;     } } 

I've tested it on three phones (none of which are rooted):

  • Samsung Galaxy S3(API 19)
  • HTC One M9(API 23)
  • Samsung Galaxy S7(API 24)

It works on the S3, but not the others. I've tried using Sensor Sense on the HTC and it doesn't work for the mic sensor. However, it used to work, and now seems to detect one sample every five seconds or so in the graph view.

Oddly enough, the microphone still works fine for phone calls and video recording on the malfunctioning phones.

1 Answers

Answers 1

You said it works on S3, which is API 19 and doesn't on those with API>=23. So, it's possible that you have a problem with runtime permissions introduced in API 23.

New behaviour (for "old apps" which use static permission model) is to return dummy data if runtime permission is not granted.

Check out this answer: Request permission for microphone on Android M

Read More

Wednesday, March 16, 2016

Android - Failed to Initialize an AudioRecord for All Possible Combinations of Arguments

Leave a Comment

I'm using the code below find an available combination of params to create an AudioRecord object.

public static AudioRecord findAudioRecord() {     for (int rate : new int[]{8000, 11025, 16000, 22050, 44100}) {         for (int audioFormat : new int[]{                 AudioFormat.ENCODING_DEFAULT,                 AudioFormat.ENCODING_PCM_16BIT,                 AudioFormat.ENCODING_PCM_8BIT,                 AudioFormat.ENCODING_PCM_FLOAT,                 AudioFormat.ENCODING_AC3,                 AudioFormat.ENCODING_E_AC3,                 AudioFormat.ENCODING_DTS,                 AudioFormat.ENCODING_DTS_HD,         }) {             for (int channelConfig : new int[]{                     AudioFormat.CHANNEL_IN_DEFAULT,                     AudioFormat.CHANNEL_IN_LEFT,                     AudioFormat.CHANNEL_IN_RIGHT,                     AudioFormat.CHANNEL_IN_FRONT,                     AudioFormat.CHANNEL_IN_BACK,                     AudioFormat.CHANNEL_IN_LEFT_PROCESSED,                     AudioFormat.CHANNEL_IN_RIGHT_PROCESSED,                     AudioFormat.CHANNEL_IN_FRONT_PROCESSED,                     AudioFormat.CHANNEL_IN_BACK_PROCESSED,                     AudioFormat.CHANNEL_IN_PRESSURE,                     AudioFormat.CHANNEL_IN_X_AXIS,                     AudioFormat.CHANNEL_IN_Y_AXIS,                     AudioFormat.CHANNEL_IN_Z_AXIS,                     AudioFormat.CHANNEL_IN_VOICE_UPLINK,                     AudioFormat.CHANNEL_IN_VOICE_DNLINK,                     AudioFormat.CHANNEL_IN_MONO,                     AudioFormat.CHANNEL_IN_STEREO}) {                 try {                     Log.d(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(MediaRecorder.AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize);                         if (recorder.getState() == AudioRecord.STATE_INITIALIZED) {                             return recorder;                         }                         recorder.release();                     }                 } catch (Exception e) {                     Log.e(TAG, rate + "Exception, keep trying.", e);                 }             }         }     }     return null; } 

But the function always returns null, with no available params found. Most combinations pass the bufferSize != AudioRecord.ERROR_BAD_VALUE condition check, but get false for recorder.getState() == AudioRecord.STATE_INITIALIZED.

I have tried to reboot my device (MOTO X2), but it didn't work.

I have declared the RECORD_AUDIO permission, and below is the manifest file:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"           package="com.example.gowear">      <uses-permission android:name="android.permission.WAKE_LOCK"/>     <uses-permission android:name="android.permission.RECORD_AUDIO" />     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>     <application         android:allowBackup="true"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:supportsRtl="true"         android:theme="@style/AppTheme">          <activity             android:name=".RecordActivity"             android:label="@string/title_activity_record"             android:theme="@style/AppTheme.NoActionBar">             <intent-filter>                 <action android:name="android.intent.action.MAIN"/>                  <category android:name="android.intent.category.LAUNCHER"/>             </intent-filter>         </activity>     </application>  </manifest> 

Below is the debug log:

03-08 20:48:58.080 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 3, channel: 16 03-08 20:48:58.091 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1 03-08 20:48:58.092 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1. 03-08 20:48:58.092 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 03-08 20:48:58.093 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 3, channel: 12 03-08 20:48:58.096 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1 03-08 20:48:58.097 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1. 03-08 20:48:58.097 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 03-08 20:48:58.097 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 3, channel: 1 03-08 20:48:58.101 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1 03-08 20:48:58.102 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1. 03-08 20:48:58.102 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 03-08 20:48:58.102 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 2, channel: 16 03-08 20:48:58.105 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1 03-08 20:48:58.106 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1. 03-08 20:48:58.106 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 03-08 20:48:58.106 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 2, channel: 12 03-08 20:48:58.109 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1 03-08 20:48:58.110 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1. 03-08 20:48:58.110 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 03-08 20:48:58.110 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 2, channel: 1 03-08 20:48:58.114 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1 03-08 20:48:58.115 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1. 03-08 20:48:58.115 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 03-08 20:48:58.115 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 1, channel: 16 03-08 20:48:58.115 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 8000 format 0 channelMask 10 03-08 20:48:58.115 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 8000, format 0, channelMask 0x10; status -22 03-08 20:48:58.115 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 1, channel: 12 03-08 20:48:58.116 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 8000 format 0 channelMask c 03-08 20:48:58.116 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 8000, format 0, channelMask 0xc; status -22 03-08 20:48:58.116 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 1, channel: 1 03-08 20:48:58.116 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 8000 format 0 channelMask 10 03-08 20:48:58.116 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 8000, format 0, channelMask 0x10; status -22 03-08 20:48:58.116 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 5, channel: 16 03-08 20:48:58.116 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 8000 format 0x9000000 channelMask 10 03-08 20:48:58.116 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 8000, format 0x9000000, channelMask 0x10; status -22 03-08 20:48:58.116 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 5, channel: 12 03-08 20:48:58.116 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 8000 format 0x9000000 channelMask c 03-08 20:48:58.116 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 8000, format 0x9000000, channelMask 0xc; status -22 03-08 20:48:58.117 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 5, channel: 1 03-08 20:48:58.117 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 8000 format 0x9000000 channelMask 10 03-08 20:48:58.117 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 8000, format 0x9000000, channelMask 0x10; status -22 03-08 20:48:58.117 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 6, channel: 16 03-08 20:48:58.117 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 8000 format 0xa000000 channelMask 10 03-08 20:48:58.117 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 8000, format 0xa000000, channelMask 0x10; status -22 03-08 20:48:58.117 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 6, channel: 12 03-08 20:48:58.117 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 8000 format 0xa000000 channelMask c 03-08 20:48:58.117 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 8000, format 0xa000000, channelMask 0xc; status -22 03-08 20:48:58.118 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 6, channel: 1 03-08 20:48:58.118 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 8000 format 0xa000000 channelMask 10 03-08 20:48:58.118 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 8000, format 0xa000000, channelMask 0x10; status -22 03-08 20:48:58.118 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 4, channel: 16 03-08 20:48:58.121 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1 03-08 20:48:58.123 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1. 03-08 20:48:58.123 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 03-08 20:48:58.123 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 4, channel: 12 03-08 20:48:58.126 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1 03-08 20:48:58.127 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1. 03-08 20:48:58.127 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 03-08 20:48:58.127 12250-12250/com.example.gowear D/Recorder: Attempting rate 8000Hz, bits: 4, channel: 1 03-08 20:48:58.133 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1 03-08 20:48:58.134 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1. 03-08 20:48:58.134 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 03-08 20:48:58.134 12250-12250/com.example.gowear D/Recorder: Attempting rate 11025Hz, bits: 3, channel: 16 03-08 20:48:58.137 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1 03-08 20:48:58.138 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1. 03-08 20:48:58.138 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 03-08 20:48:58.139 12250-12250/com.example.gowear D/Recorder: Attempting rate 11025Hz, bits: 3, channel: 12 03-08 20:48:58.141 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1 03-08 20:48:58.149 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1. 03-08 20:48:58.149 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 03-08 20:48:58.149 12250-12250/com.example.gowear D/Recorder: Attempting rate 11025Hz, bits: 3, channel: 1 03-08 20:48:58.152 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1 03-08 20:48:58.154 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1. 03-08 20:48:58.154 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 03-08 20:48:58.154 12250-12250/com.example.gowear D/Recorder: Attempting rate 11025Hz, bits: 2, channel: 16 03-08 20:48:58.157 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1 03-08 20:48:58.159 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1. 03-08 20:48:58.159 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 03-08 20:48:58.159 12250-12250/com.example.gowear D/Recorder: Attempting rate 11025Hz, bits: 2, channel: 12 03-08 20:48:58.161 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1 03-08 20:48:58.163 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1. 03-08 20:48:58.163 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 03-08 20:48:58.163 12250-12250/com.example.gowear D/Recorder: Attempting rate 11025Hz, bits: 2, channel: 1 03-08 20:48:58.166 12250-12250/com.example.gowear E/AudioRecord: AudioFlinger could not create record track, status: -1 03-08 20:48:58.167 12250-12250/com.example.gowear E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -1. 03-08 20:48:58.167 12250-12250/com.example.gowear E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 03-08 20:48:58.167 12250-12250/com.example.gowear D/Recorder: Attempting rate 11025Hz, bits: 1, channel: 16 03-08 20:48:58.168 12250-12250/com.example.gowear E/AudioSystem: AudioSystem::getInputBufferSize failed sampleRate 11025 format 0 channelMask 10 03-08 20:48:58.168 12250-12250/com.example.gowear E/AudioRecord: AudioSystem could not query the input buffer size for sampleRate 11025, format 0, channelMask 0x10; status -22 

...

I have read lots of similar questions but none of them could fix my problem. Is there any other solution? Thanks!

0 Answers

Read More