Turning the computer volume up/down with Java?

风流意气都作罢 提交于 2020-01-01 14:38:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!