简体   繁体   English

JLayer 播放时改变音量 (JavaZoom)

[英]JLayer Change volume while playing (JavaZoom)

I build a JLayer that plays a web music stream, which works fine, except for the volume.我构建了一个播放 web 音乐 stream 的 JLayer,它工作正常,除了音量。 I cant get it to change.我不能让它改变。

I already tried the following, but the volume always remains the same, no errors:我已经尝试了以下方法,但音量始终保持不变,没有错误:

public synchronized void setVolume(float vol) {
    Line.Info source = Port.Info.SPEAKER;

    if (AudioSystem.isLineSupported(source)) {
        try {
            Port outline = (Port) AudioSystem.getLine(source);
            outline.open();

            FloatControl volumeControl = (FloatControl) outline.getControl(FloatControl.Type.VOLUME);
            System.out.println("volume: " + volumeControl.getValue());

            volumeControl.setValue(vol);
            System.out.println("new volume: " + volumeControl.getValue());

        } catch (LineUnavailableException ex) {
            System.err.println("source not supported");
            ex.printStackTrace();
        }
    }
}


/*public synchronized void setVolume(double vol) {
    try {
        Mixer.Info[] infos = AudioSystem.getMixerInfo();
        for (Mixer.Info info : infos) {
            Mixer mixer = AudioSystem.getMixer(info);
            if (mixer.isLineSupported(Port.Info.SPEAKER)) {
                Port port = (Port) mixer.getLine(Port.Info.SPEAKER);
                port.open();
                if (port.isControlSupported(FloatControl.Type.VOLUME)) {
                    FloatControl volume = (FloatControl) port.getControl(FloatControl.Type.VOLUME);
                    volume.setValue((float) (vol / 100));
                }
                port.close();
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}*/

My experience is that the implementation of the various Controls supported by Java is somewhat hit-or-miss.我的经验是,Java 支持的各种Controls的实现有些成败。 I'm not clear why this should be.我不清楚为什么会这样。 Perhaps it has to do with the capabilities supported by the OS or particular machine?也许它与操作系统或特定机器支持的功能有关?

In any event, for volume, the MASTER_GAIN is usually the best bet for volume.无论如何,对于交易量, MASTER_GAIN通常是交易量的最佳选择。

The ending portion of the Java Tutorials section on using Controls discusses manipulating the audio data directly. Java 教程部分关于使用控件的结尾部分讨论了直接操作音频数据。 I recommend this option, but getting at the data when using the JavaZoom code can be problematic, and probably involves tweaking the source code of the library.我推荐这个选项,但是在使用 JavaZoom 代码时获取数据可能会出现问题,并且可能涉及调整库的源代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM