How to throw an exception when accessing AudioRecord?

can't test on a real device, only the emulator, APIs, 28,29, permission for microphone is not issued
var recorder: AudioRecord? = null
 try {
 val minBufferSize = AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT)

 require(!(minBufferSize == AudioRecord.ERROR || minBufferSize == AudioRecord.ERROR_BAD_VALUE))
 recorder = AudioRecord(
MediaRecorder.AudioSource.VOICE_COMMUNICATION,
8000,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
 minBufferSize * 10)

 // Wait until audio record is initialized.
 var waitCycles = 20
 while (recorder.state != AudioRecord.STATE_INITIALIZED && waitCycles > 0) {
Thread.sleep(250)
 waitCycles -= 1
}
 if (recorder.state != AudioRecord.STATE_INITIALIZED) {
 throw Exception("Unable to initialize AudioRecord instance")
}

 return recorder

I expect that when creating AudioRecord SecurityException should work, but just get an uninitialized AudioRecord as to cause SecurityException?
April 3rd 20 at 17:16
1 answer
April 3rd 20 at 17:18
Solution
this is a bug in the API, the ancient code of the recorder, no one is going to rewrite

Find more questions by tags Android