如何用Java更改Master Volume?

时间:2017-01-30 19:21:53

标签: java audio

我试图编写一个小程序来改变给定设备的主音量。目前它看起来像这样

import java.util.*;
import javax.sound.sampled.*;

public class VolumeControl {

    public static void main(String[] args) throws Exception {
        Mixer mixer = findMixer(args[0]);

        changeVolume(mixer, Integer.valueOf(args[1]));
    }

    private static Mixer findMixer(String name) {
        Mixer.Info mixerInfo = Arrays.stream(AudioSystem.getMixerInfo())
            .filter(info -> name.equals(info.getName()))
            .findFirst()
            .orElseThrow(() -> new IllegalArgumentException(String.format("no mixer with the name '%s' found", name)));
        return AudioSystem.getMixer(mixerInfo);
    }

    private static void changeVolume(Mixer mixer, int level) throws LineUnavailableException {
        for(Line.Info info : mixer.getSourceLineInfo()) {
            try(Line line = mixer.getLine(info)) {
                if (!line.isOpen()) line.open();

                FloatControl control = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
                control.setValue(limit(control.getMinimum(), control.getMaximum(), (float) level));
            }
        }
    }

    private static float limit(float min, float max, float level) {
        return Math.min(max, Math.max(min, level));
    }
}

当我用我的设备名编译并运行它时,我总是得到以下异常

Exception in thread "main" java.lang.IllegalArgumentException: illegal call to open() in interface Clip
        at com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(Unknown Source)
        at com.sun.media.sound.AbstractDataLine.open(Unknown Source)
        at com.sun.media.sound.AbstractDataLine.open(Unknown Source)
        at VolumeControl.changeVolume(VolumeControl.java:23)
        at VolumeControl.main(VolumeControl.java:9)

我做错了吗?我在互联网上搜索了这个错误,但没有找到任何有用的东西。

如何摆脱此异常和/或了解这实际意味着什么?

1 个答案:

答案 0 :(得分:0)

我已更新了我的代码。现在它看起来像这个

import java.util.*;
import javax.sound.sampled.*;

public class VolumeControl {

    public static void main(String[] args) throws Exception {
        Mixer mixer = findMixer(args[0]));

        changeVolume(mixer, Integer.valueOf(args[1]));
    }

    private static Mixer findMixer(String name) {
        Mixer.Info mixerInfo = Arrays.stream(AudioSystem.getMixerInfo())
            .filter(info -> name.equals(info.getName()))
            .findFirst()
            .orElseThrow(() -> new IllegalArgumentException(String.format("no mixer with the name '%s' found", name)));
        return AudioSystem.getMixer(mixerInfo);
    }

    private static void changeVolume(Mixer mixer, int level) throws LineUnavailableException {
        for(Line.Info info : mixer.getSourceLineInfo()) {
            Line line = mixer.getLine(info);
            boolean shouldOpen = !(line.isOpen() || line instanceof Clip);
            try {
                if (shouldOpen) line.open();

                FloatControl control = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
                control.setValue(limit(control.getMinimum(), control.getMaximum(), (float) level));
            } finally {
                if (shouldOpen) {
                    line.close();
                }
            }
        }
    }

    private static float limit(float min, float max, float level) {
        return Math.min(max, Math.max(min, level));
    }
}

现在我没有任何例外,但主音量无论如何都没有改变。

我也尝试使用mixer.getTargetLineInfo(),但音量也没有改变。

我在Windows Vista中看到了一个新的声音架构,所以我尝试创建一个exe文件并在Windows XP兼容模式下运行它,结果相同。

经过一些研究后,我发现this class - 正如预期的那样 - 也没有用。当getMasterOutputLine()方法返回null时,它给了我一个null的主卷。

我没有胶水下一步做什么:( 也许你们中的任何人都可以给我一些提示甚至是SSCCE。

相关问题