4

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
xR34P3Rx
  • 395
  • 9
  • 28

2 Answers2

4

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();  
            }  
        }  
    }  
} 
syb0rg
  • 8,057
  • 9
  • 41
  • 81
  • 4
    Consider putting some of the information from that link within the body of your answer. – arshajii Jan 12 '13 at 23:13
  • Yes that's much better, thanks :) I only asked because link-only answers aren't considered particularly good. – arshajii Jan 12 '13 at 23:21
  • Ya, sorry about that. I just didn't want to copy to much information from the link and then get down-voted for it. – syb0rg Jan 12 '13 at 23:22
  • See [Can Java Sound be used to control the system volume?](http://stackoverflow.com/q/14301618/418556) for the bad news re. that source (and this question in general). – Andrew Thompson Jan 13 '13 at 07:48
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

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Unfortunately none of the Java Sound controls that are available actually affect the system volume. See my linked Q&A for details. – Andrew Thompson Jan 13 '13 at 07:52