我在how to implement audio sounds in Java games上观看了Mattew的精彩教程。
问题是,即使在我运行游戏后减少了wav文件的音量,在Java中wav声音文件的音量仍然非常高,我的意思是你甚至无法玩游戏因为背景音乐太大了。
为什么wav文件的卷不能用Java维护?
答案 0 :(得分:2)
使用基于Java Sound的Clip
可能比基于applet的AudioClip
更好。 Clip
接口支持控件,其中一个控件应为MASTER_GAIN
。
E.G。
import java.awt.*;
import java.net.URL;
import javax.swing.*;
import javax.swing.event.*;
import javax.sound.sampled.*;
class ClipVolume {
public static void main(String[] args) throws Exception {
URL url = new URL(
"http://pscode.org/media/leftright.wav");
final Clip clip = AudioSystem.getClip();
// getAudioInputStream() also accepts a File or InputStream
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
clip.open(ais);
clip.loop(Clip.LOOP_CONTINUOUSLY);
Runnable r = new Runnable() {
@Override
public void run() {
final FloatControl control = (FloatControl)
clip.getControl(FloatControl.Type.MASTER_GAIN);
final JSlider volume = new JSlider(
JSlider.HORIZONTAL,
(int) control.getMinimum(),
(int) control.getMaximum(),
(int) control.getValue());
volume.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent ce) {
control.setValue(volume.getValue());
}
});
JOptionPane.showMessageDialog(null, volume);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}
答案 1 :(得分:0)
所以,这是我正在使用的代码,正在运行并希望这也将有助于其他人。 快乐的编码,再次感谢Andrew Thompson
package com.stefanbanu;
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
// To play sound using Clip, the process need to be alive.
// Hence, we use a Swing application.
public class SoundClipTest extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
// Constructor
public SoundClipTest() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Test Sound Clip");
this.setSize(300, 200);
this.setVisible(true);
try {
// Open an audio input stream.
URL url = this.getClass().getClassLoader().getResource("bgsong.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
// Get a sound clip resource.
Clip clip = AudioSystem.getClip();
// Open audio clip and load samples from the audio input stream.
clip.open(audioIn);
clip.start();
final FloatControl control = (FloatControl)
clip.getControl(FloatControl.Type.MASTER_GAIN);
control.setValue(-30.0f);
// final JSlider volume = new JSlider(
// JSlider.HORIZONTAL,
// (int) control.getMinimum(),
// (int) control.getMaximum(),
// (int) control.getValue());
// volume.addChangeListener(new ChangeListener() {
//
// public void stateChanged(ChangeEvent ce) {
// control.setValue(volume.getValue());
// }
// });
//
// JOptionPane.showMessageDialog(null, volume);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new SoundClipTest();
}
}