Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether microphone is used by any background app

Tags:

android

I have been searching for couple of days now and havent been able to find a suitable solution.

I am trying to check if any app in the background is using the microphone, so my app can use it, otherwise i want just to show message "Microphone in use by another app".

I tried checking all the applications in the background and their permissions but that doesnt solve my problem, since there is package wearable.app which asks for the permissions but it doesnt affect the audio, or it is not using it.

I tried the other solutions that i was able to find here or on google, but none of that seems to be the proper way.

All i want to check if the microphone is not being used, so my app can use it.

Any suggestion i will appreciate.

like image 301
Borce Ivanovski Avatar asked Feb 25 '16 16:02

Borce Ivanovski


4 Answers

After searching more i found the solution and i am adding it here for anyone that needs it to find it easier.

private boolean validateMicAvailability(){
    Boolean available = true;
    AudioRecord recorder =
            new AudioRecord(MediaRecorder.AudioSource.MIC, 44100,
                    AudioFormat.CHANNEL_IN_MONO,
                    AudioFormat.ENCODING_DEFAULT, 44100);
    try{
        if(recorder.getRecordingState() != AudioRecord.RECORDSTATE_STOPPED ){
            available = false;

        }

        recorder.startRecording();
        if(recorder.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING){
            recorder.stop();
            available = false;

        }
        recorder.stop();
    } finally{
        recorder.release();
        recorder = null;
    }

    return available;
}
like image 170
Borce Ivanovski Avatar answered Sep 22 '22 12:09

Borce Ivanovski


You can do it the other way around.

Get the microphone in your app.

Get a list of the installed apps, who have a RECORD permission.

Then check if one of these apps is on the foreground and if there is one release the microphone so that the other app can use it (for example when a phone call occurs).

A bit dirty practice but I think it is what you are looking for.

Cheers!

like image 29
Konstantinos Michael Avatar answered Sep 20 '22 12:09

Konstantinos Michael


This is how is done

AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);

if(am.getMode()==AudioManager.MODE_IN_COMMUNICATION){
   //Mic is in use
}

MODE_NORMAL -> You good to go. Mic not in use
MODE_RINGTONE -> Incoming call. The phone is ringing
MODE_IN_CALL -> A phone call is in progress
MODE_IN_COMMUNICATION -> The Mic is being used by another application
like image 34
Osagui Aghedo Avatar answered Sep 22 '22 12:09

Osagui Aghedo


AudioManager.AudioRecordingCallback()

        am.registerAudioRecordingCallback(new AudioManager.AudioRecordingCallback() {
        @Override
        public void onRecordingConfigChanged(List<AudioRecordingConfiguration> configs) {
            super.onRecordingConfigChanged(configs);
            try {
                isMicOn = configs.get(0) != null;
            }catch (Exception e)
            {
                isMicOn = false;
            }
            if (isMicOn) {
                //microphone is on
            } else {
                // microphone is off
            }
            Toast.makeText(context, isMicOn ? "Mic on" : "Mic off", Toast.LENGTH_SHORT).show();

        }
    }, null);
like image 31
Abdalla Tawfik Avatar answered Sep 22 '22 12:09

Abdalla Tawfik