Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if android microphone is available for use?

Tags:

android

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?

like image 792
rabz100 Avatar asked Mar 27 '14 03:03

rabz100


People also ask

Where do I find Microphone settings on Android?

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.

How do I know if my Microphone is being used?

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.

Why isn't my mic working on my Android?

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.


2 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;
    }
like image 96
Adhikari Bishwash Avatar answered Nov 15 '22 08:11

Adhikari Bishwash


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
}
like image 39
Alex Reye Avatar answered Nov 15 '22 08:11

Alex Reye