问题
I want to turn the master volume of the computer up and down (100%/0%), with just a command.
I saw that I could use FloatControl
, but I'm not sure how to use it.
回答1:
Have at look at using JavaSound to control the master volume.
From the link:
Mixer.Info [] mixers = AudioSystem.getMixerInfo();
System.out.println("There are " + mixers.length + " mixer info objects");
for (Mixer.Info mixerInfo : mixers)
{
System.out.println("Mixer name: " + mixerInfo.getName());
Mixer mixer = AudioSystem.getMixer(mixerInfo);
Line.Info [] lineInfos = mixer.getTargetLineInfo(); // target, not source
for (Line.Info lineInfo : lineInfos)
{
System.out.println(" Line.Info: " + lineInfo);
Line line = null;
boolean opened = true;
try
{
line = mixer.getLine(lineInfo);
opened = line.isOpen() || line instanceof Clip;
if (!opened)
{
line.open();
}
FloatControl volCtrl = (FloatControl)line.getControl(FloatControl.Type.VOLUME);
System.out.println(" volCtrl.getValue() = " + volCtrl.getValue());
}
catch (LineUnavailableException e)
{
e.printStackTrace();
}
catch (IllegalArgumentException iaEx)
{
System.out.println(" " + iaEx);
}
finally
{
if (line != null && !opened)
{
line.close();
}
}
}
}
回答2:
FloatControl
can be used when playing a specific audio clip:
AudioInputStream audioInputStream =
AudioSystem.getAudioInputStream(new File("MyClip.wav"));
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
FloatControl volumeControl =
(FloatControl) clip.getControl(FloatControl.Type.VOLUME);
volumeControl.setValue(10.0f); // Increase volume by 10 decibels.
clip.start();
Also see: Processing Audio with Controls
来源:https://stackoverflow.com/questions/14298917/turning-the-computer-volume-up-down-with-java