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?
-
As a mix of ideas of answers, from what I've just coded, I think you should use AudioRecord for normal audio sources, and MediaRecorder for non-normal sources, like `VOICE_CALL`. MR needs a file even to check the source, as opposite to AR, so should be secondary, as the user will need to give you permission to write a file just to check an audio source, which is weird. AR doesn't need this, but it won't let you use non-normal audio sources. Use MR if AR throws an exception or doesn't initialize, for example. – Edw590 Dec 28 '20 at 02:37
-
Actually, we can create a file in the Cache directory, which doesn't require any permissions. Though I think still checking for an exception on `start()` which I see happening but I don't see documented is not as good as checking if AudioRecord is recording or not with the specific method (which doesn't exist in MediaRecorder and we're forced to check for "non-existant"(?) exceptions). Anyone please correct me if something I said is wrong, like the "non-existant" exception, but I don't see it anywhere and it's still thrown. – Edw590 Dec 28 '20 at 02:59
-
Please find my complete solution for checking mic availability in different Android versions in this answer: https://stackoverflow.com/a/75510613/3873867 – Nijat Ahmadli Feb 22 '23 at 08:59
3 Answers
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;
}

- 410
- 1
- 6
- 15

- 2,770
- 28
- 32
-
Thanks for the idea. The microphone will actually prepare fine even if the microphone is in use. So what I did was catch the exception thrown by start. If you add this to your answer I will accept it. – rabz100 Mar 27 '14 at 04:12
-
3I don't think it's a good practive to put `Exception`. You could use `RuntimeException`, which is the one that is thrown when the audio source isn't available. Could be important and you're missing it. Also, `prepare()` throws other exceptions you'll be missing, like being called after start() and before setOutputFile() or if the prepare fails. At least in my case, it can always prepare, but it won't start if the source isn't available. So I catch `RuntimeException` to see if it's available or not on `start()`. Other exceptions shouldn't happen and we should fix them manually (could be wrong). – Edw590 Dec 28 '20 at 02:32
-
1
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
}

- 41
- 4
-
-
1@MahmoudMabrok You must create it with the AudioRecord constructor. Look here: https://developer.android.com/reference/android/media/AudioRecord. – Edw590 Dec 28 '20 at 02:25
I also want to detect whether microphone is being used or not. My solution is using AudioManager to get current mode.
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (am.getMode() == AudioManager.MODE_NORMAL){
//microphone is available.
}
Other modes usage like MODE_IN_COMMUNICATION, MODE_IN_CALL, please check https://developer.android.com/reference/android/media/AudioManager.html#MODE_NORMAL

- 9
- 2
-
2Your solution is not correct. Mode could be still at MODE_NORMAL even when the microphone is captured by some app, like an audio recorder. – std.denis Jan 30 '17 at 16:39