关闭电脑音量加/减与Java?(Turning the computer volume up/dow

2019-08-06 15:29发布

我想打开计算机的主音量向上和向下(100%/ 0%),只用一个命令。 我看到了,我可以用FloatControl ,但我不知道如何使用它。

Answer 1:

有在看用JavaSound来控制主音量 。

从链接:

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();  
            }  
        }  
    }  
} 


Answer 2:

FloatControl可以播放特定的音频剪辑时,可以使用:

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();

另请参阅: 与控制处理,音频



文章来源: Turning the computer volume up/down with Java?