the microphone in my Device

65 views Asked by At

My app is recording voice, but when another app uses the microphone,
(for example, WhatsApp) then the WhatsApp cannot use microphone because the microphone is being used by my app.

How can I let the microphone share audio with other applications and my app?

1

There are 1 answers

2
Vikasdeep Singh On

Unfortunately another app can not use microphone resource when already one app is using it.

The case is similar to Camera, can not be used by two apps at the same time.

Solution:

Check if microphone is available If not available then show some message that "currently microphone is in use by some other app and can't be used".

Code to check availability of microphone:

private boolean isMicrophoneAvailable() {
    Boolean isAvailable = true;
    AudioRecord recorder =
            new AudioRecord(MediaRecorder.AudioSource.MIC, 44100,
                    AudioFormat.CHANNEL_IN_MONO,
                    AudioFormat.ENCODING_DEFAULT, 44100);
    try {
        if (recorder.getRecordingState() != AudioRecord.RECORDSTATE_STOPPED) {
            isAvailable = false;
        }
        recorder.startRecording();
        if (recorder.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
            recorder.stop();
            isAvailable = false;
        }
        recorder.stop();
    } finally {
        recorder.release();
        recorder = null;
    }
    return isAvailable;
}