I am currently programming an app that uses the microphone to query the sound level. I am using AlarmManager to query the sound level every minute. The problem I am facing is that I discovered if I am using another app that uses the microphone too (e.g. decibel level reader) my app will crash because the microphone is not available. Is there a way to check if the microphone is currently being used or not?
What to Know. Tap Settings > Privacy > App Permissions > Microphone to toggle which apps can use your microphone. If your call is muted, tap Mute so you can speak again. If your microphone doesn't work, check that there are no obstructions.
Head to Settings > Privacy > Microphone in Windows 10 to open the microphone settings. Scroll through the list of applications with permission to access your microphone and look under each for a “Last accessed” date and time.
Go to Settings > Apps & notifications > Advanced > Permission manager > Microphone. If you have installed sound amplifier or enhancer apps, they are most likely the culprit. Otherwise, try checking the apps that you suspect are the cause, revoke their mic access, then test your mic to see if they were the cause.
Try Catching exception, as you get exception when you try to use microphone you can handle it.
"The microphone will actually prepare fine even if the microphone is in use"
OR this code snippet may give you an idea
//returns whether the microphone is available
public static boolean getMicrophoneAvailable(Context context) {
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile(new File(context.getCacheDir(), "MediaUtil#micAvailTestFile").getAbsolutePath());
boolean available = true;
try {
recorder.prepare();
recorder.start();
}
catch (Exception exception) {
available = false;
}
recorder.release();
return available;
}
if you use AudioRecord
then call startRecording()
and after that you should check recorder's state: getRecordingState()
. If recording was started successfully (it means mic is available), it will return 3 (AudioRecord.RECORDSTATE_RECORDING
) otherwise it will return 1 (AudioRecord.RECORDSTATE_STOPPED
)
Here's code for this function in Kotlin:
private fun isMicAvailable(audioRecord: AudioRecord): Boolean {
audioRecord.startRecording()
val isAvailable = audioRecord.recordingState == AudioRecord.RECORDSTATE_RECORDING
audioRecord.stop()
audioRecord.release()
return isAvailable
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With