Mitya  XMitya
Mitya XMitya

Reputation: 1139

List of connected audio input devices on Android

My application must record sound only from external microphone. Is there a way to get list of available microphones?

I'm using AudioRecord for sound capturing.

Thanks in advance.

Upvotes: 3

Views: 2658

Answers (1)

Mitya  XMitya
Mitya XMitya

Reputation: 1139

The applicable solution for me I found here. And added the following code:

private static boolean headsetMic;

private static BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
            final int headphones = intent.getIntExtra("state", -1);
            final int mic = intent.getIntExtra("microphone", -1);
            // we need to check both of them, because if headset with
            // mic was disconnected mic is 1 but headphones is 0, and
            // actually no external mic is connected
            headsetMic = headphones > 0 && mic > 0;
        }
    }
};

public static boolean isExternalMicConnected() {
    return headsetMic;
}

I call isExternalMicConnected() and it shows if custom mic was connected.

Upvotes: 3

Related Questions