I'm trying to write a small program in order to change the master volume of a given device. Currently it looks like this
import java.util.*;
import javax.sound.sampled.*;
public class VolumeControl {
public static void main(String[] args) throws Exception {
Mixer mixer = findMixer(args[0]);
changeVolume(mixer, Integer.valueOf(args[1]));
}
private static Mixer findMixer(String name) {
Mixer.Info mixerInfo = Arrays.stream(AudioSystem.getMixerInfo())
.filter(info -> name.equals(info.getName()))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(String.format("no mixer with the name '%s' found", name)));
return AudioSystem.getMixer(mixerInfo);
}
private static void changeVolume(Mixer mixer, int level) throws LineUnavailableException {
for(Line.Info info : mixer.getSourceLineInfo()) {
try(Line line = mixer.getLine(info)) {
if (!line.isOpen()) line.open();
FloatControl control = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
control.setValue(limit(control.getMinimum(), control.getMaximum(), (float) level));
}
}
}
private static float limit(float min, float max, float level) {
return Math.min(max, Math.max(min, level));
}
}
When I compile and run this with my device name I always get the following exception
Exception in thread "main" java.lang.IllegalArgumentException: illegal call to open() in interface Clip
at com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(Unknown Source)
at com.sun.media.sound.AbstractDataLine.open(Unknown Source)
at com.sun.media.sound.AbstractDataLine.open(Unknown Source)
at VolumeControl.changeVolume(VolumeControl.java:23)
at VolumeControl.main(VolumeControl.java:9)
Am I doing anything wrong? I searched for this error on the internet but didn't found anything useful.
How can I get rid of this exception and/or understand what this actually means?