Java - input sound line has no controls

Description

Mac OS X 10.10.3

An attempt to control the microphone (basically, setting the microphone volume to 100% + in the hope of turning it on and trying to solve this problem https://discussions.apple.com/message/29309845#29309845 ).

(improved code)

import javax.sound.sampled.*;
public class MicMacMax {
    public static void main(String[] args) {
        Mixer.Info microphone = null;
        for (Mixer.Info info : AudioSystem.getMixerInfo()) {
            System.out.println("Potential mixer: " + info.getName());
            if (info.getName().equals("Built-in Input")) {
                microphone = info;
            }
        }
        if (microphone == null) {
            System.out.println("Microphone not found");
            System.exit(-1);
        }
        final Mixer mixer = AudioSystem.getMixer(microphone);
        try {
            mixer.open();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        final Line lineIn;
        try {
            lineIn = mixer.getLine(mixer.getTargetLineInfo()[0]);
            lineIn.open();
            if (lineIn.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
                System.out.println("Gain supported");
            }
            final CompoundControl cc = (CompoundControl) lineIn.getControls()[0];
            final Control[] controls = cc.getMemberControls();
            for (final Control c : controls) {
                if (c instanceof FloatControl) {
                    System.out.println("BEFORE LINE_IN VOL = "
                                       + ((FloatControl) c).getValue());
                    ((FloatControl) c).setValue((float) 100 / 100);
                    System.out.println("AFTER LINE_IN VOL = "
                                       + ((FloatControl) c).getValue());
                }
            }
        } catch (final Exception e) {
            e.printStackTrace();
        }
    }
}

This code generates:

java.lang.ArrayIndexOutOfBoundsException: 0 on MicMacMax.main (MicMacMax.java:24)

This means that there are no controls for the line.

Help needed

  • Are there any errors in my code?

  • It would be great if someone could check this code on their machine and provide feedback . I would like to highlight the problem.

  • Could this be due to any permission issue?
+4
2

, (). , . , , . , 0, , , , .

, :

Potential mixer: Primary Sound Driver
Potential mixer: Speaker/HP (Realtek High Definition Audio)
Potential mixer: Primary Sound Capture Driver
Potential mixer: Microphone (Realtek High Defini
Potential mixer: Stereo Mix (Realtek High Defini
Potential mixer: Port Speaker/HP (Realtek High Defini
Potential mixer: Port Microphone (Realtek High Defini
Potential mixer: Port Stereo Mix (Realtek High Defini
Microphone not found

, .

+1

, . 100 , 100 .

import javax.sound.sampled.*;
public class MicMacMax {
public static void main(String[] args) {
    //pass volume level as parameter
    setMicrophoneVolume(100);
}
public static void setMicrophoneVolume(int value) {
    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;
        FloatControl volCtrl = null;
        if (maxLines > 0) {
            try {
                lineIn = (Port) mixer.getLine(Port.Info.MICROPHONE);
                lineIn.open();
                CompoundControl cc = (CompoundControl) lineIn.getControls()[0];
                Control[] controls = cc.getMemberControls();
                for (Control c : controls) {
                    if (c instanceof FloatControl) {
                        volCtrl = (FloatControl) c;
                        volCtrl.setValue((float) value / 100);
                    }
                }

            } catch (Exception ex) {
                continue;
            }
        }
    }
}

}

, .

+1

Source: https://habr.com/ru/post/1615835/


All Articles