Java check if microphone input is muted

Viewed 619

I'm trying to make a simple program that checks if a microphone is muted or not. This is the code I have for determine which Line the microphone is.

private Line getMic() throws LineUnavailableException {

    Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();

    for (int i = 0; i < mixerInfos.length; i++) {
        Mixer mixer = AudioSystem.getMixer(mixerInfos[i]);
        int maxLines = mixer.getMaxLines(Port.Info.MICROPHONE);
        Port lineIn = null;
        if (maxLines > 0) {

            lineIn = (Port) mixer.getLine(Port.Info.MICROPHONE);
            return lineIn;
        }
    }

    return null;
}

Then from there I call the following:

BooleanControl muteControl = (BooleanControl)mic.getControl(BooleanControl.Type.MUTE);
System.out.println(muteControl.getValue());

However, I'm getting an error:

Unsupported control type: Mute

I went ahead and looked to see what Controls were available for the Line using the following:

for(Control c : mic.getControls()){
    System.out.println(c.getType());
}    

There is only one Control and it is the Master Volume. I'm not sure why the Mute control is not listed.

The line that is being returned in my getMic() function is returning the correct microphone. I have a USB headset plugged in and it is recognized by any application.

1 Answers

The various audio control lines are not guaranteed to be implemented. I'm not sure why, but I think it has to do with the OS and local audio software which can vary considerably.

From Processing Audio with Controls (the last section Manipulating the Audio Data Directly):

The Control API allows an implementation of the Java Sound API, or a third-party provider of a mixer, to supply arbitrary sorts of signal processing through controls. But what if no mixer offers the kind of signal processing you need? It will take more work, but you might be able to implement the signal processing in your program. Because the Java Sound API gives you access to the audio data as an array of bytes, you can alter these bytes in any way you choose.

For this reason, I tend to avoid using the control lines, and where possible, write code to accomplish the desired functionality. For example, one can manually access the individual frames of a SourceDataLine or TargetDataLine.

IDK how to test for "mute" on the microphone, but testing for incoming volume is doable using an RMS algorithm on the incoming PCM data points.

Related