问题描述
我想尝试使用 JSlider 作为音量控制,但不太知道如何继续。
JSlider、AudioInputStream & FloatControl 部分:
public static JSlider slider;
public static JMenuBar bar;
public static Clip mii;
// JMenu Sound
JMenu sound = new JMenu("Sound");
sound.setFont(new Font("Courier New",Font.BOLD,12));
bar.add(sound);
slider = new JSlider(JSlider.HORIZONTAL,100,50);
slider.setMinorTickSpacing(10);
slider.setMajorTickSpacing(20);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.addChangeListener(new Sound_BackgroundAudio());
sound.add(slider);
// Background audio
try {
File audio_mii = new File("src/main/resources/mii.wav");
AudioInputStream mii_audioInputStream = AudioSystem.getAudioInputStream(audio_mii);
mii = AudioSystem.getClip();
mii.open(mii_audioInputStream);
mii_floatControl = (FloatControl) mii.getControl(FloatControl.Type.MASTER_GAIN);
mii.loop(Clip.LOOP_CONTINUOUSLY);
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
String[] options = {"Continue"};
JOptionPane.showOptionDialog(frame,ex,eE,JOptionPane.YES_OPTION,JOptionPane.ERROR_MESSAGE,null,options,options[0]);
}
Sound_BackgroundAudio 类:
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Sound_BackgroundAudio implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
//Main.mii_floatControl.setValue(20f * (float) Math.log10(Main.slider.getValue()) / 10);
Main.mii_floatControl.setValue(Main.slider.getValue() / 10);
System.out.println("" + Main.mii_floatControl.getValue());
}
}
在转换为 Main.mii_floatControl.setValue() 时,我已经尝试了很多 计算出合适的值,但体积既没有改变,也不知道正确的公式。也许我必须以完全不同的方式来做?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)