设置 Java Clip 的音量 [英] Set volume of Java Clip

查看:34
本文介绍了设置 Java Clip 的音量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在Java中设置Clip的各自音量?

Is there any way to set the respective volume of a Clip in Java?

我有这个方法:

public static void play(Clip clip) {
    if (Settings.getSettings().isVolumeOn()) {
        FloatControl volume = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
        volume.setValue(-1 * Settings.getSettings().getVolume());
        clip.start();
    }
}

Settings.getSettings().getVolume() 返回 0 - 100

卷:

  1. 0:没有声音
  2. 40:使用耳机获得最佳音效
  3. 60:最佳音效
  4. 100:全音

所以本质上这应该是 VLC 的比例(但一半因为 VLC 是从 0 到 200).

So essentially this should be like the scale of VLC (but half since VLC is from 0 to 200).

我发现我可以通过使用volume.setValue(-10f);

但我更喜欢 volume.setValue(clip.getMaxVolume() * Settings.getSettings().getVolume()/100) 类型的东西.

But I would prefer something of the type volume.setValue(clip.getMaxVolume() * Settings.getSettings().getVolume()/100).

其中 clip.getMaxVolume() 将返回剪辑的最大音量.

Where clip.getMaxVolume() would return the max volume of the clip.

推荐答案

根据Killer Game Programming by Andrew Davison,您可以这么简单地调节音量:

According to Killer Game Programming by Andrew Davison you can adjust volume this simple:

float range = gainControl.getMaximum() - gainControl.getMinimum();
float gain = (range * volume) + gainControl.getMinimum();
gainControl.setValue(gain);

volume 是所需的浮动音量(0.0f 表示没有声音,1.0f 表示完整音频)gainControlFloatControl

volume being the desired volume in float (0.0f means no sound, 1.0f means full audio) gainControl is the FloatControl

希望你能让它发挥作用!

Hope you can get it to work!

这篇关于设置 Java Clip 的音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆