问题
I am fairly new at java. I need to control volume (volume up/down, mute) in a java application. I couldn't find a way to do this. I am developing on a linux system (for information).
I tired this code:
Java Code:
Port lineIn;
FloatControl volCtrl;
try {
mixer = AudioSystem.getMixer(null);
lineIn = (Port)mixer.getLine(Port.Info.LINE_IN);
lineIn.open();
volCtrl = (FloatControl) lineIn.getControl(
FloatControl.Type.VOLUME);
// Assuming getControl call succeeds,
// we now have our LINE_IN VOLUME control.
} catch (Exception e) {
System.out.println("Failed trying to find LINE_IN"
+ " VOLUME control: exception = " + e);
}
but i got execption
Failed trying to find LINE_IN VOLUME control: exception = java.lang.IllegalArgumentException: Line unsupported: COMPACT_DISC source port
Thanks for your help
回答1:
Sometimes the controls are nested, which makes a platform independent solution difficult. I have written an utility class which is used like that:
Audio.setMasterOutputVolume(0.5f);
Source code is here: https://github.com/Kunagi/ilarkesto/blob/master/src/main/java/ilarkesto/media/Audio.java
Stackoverflow users can use the code from Audio.java under the terms of the WTFPL.
回答2:
I don't think there is a constant called VOLUME. Its MASTER_GAIN. So you should do
volCtrl = (floatControl) lineIn.getControl(FloatControl.Type.MASTER_GAIN);
volCtrl.setValue(you_custom_value);
来源:https://stackoverflow.com/questions/7956115/controlling-volume-in-a-java-application