我有一个游戏需要能够播放声音。当我点击一个按钮时,声音会播放,但当我再次点击该按钮时,声音不会播放。
下面是我的声音等级:
public class Sound {
public static final Sound select = new Sound("/selectBTN.wav");
public static final Sound scroll = new Sound("/btn.wav");
//Phone calls
public static final Sound call1 = new Sound("/calls/call1.wav");
public static final Sound call2 = new Sound("/calls/call2.wav");
public static final Sound call3 = new Sound("/calls/call3.wav");
private Clip c;
public Sound(final String filename) {
try {
c = AudioSystem.getClip();
final AudioInputStream inputStream AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream(filename));
c.open(inputStream);
c.addLineListener(new LineListener() {
@Override
public void update(final LineEvent event) {
if (event.getType().equals(Type.STOP)) {
System.out.println("Do something");
}
}
});
} catch (final Exception e) {
e.printStackTrace();
}
}
public void play() {
try {
new Thread() {
public void run() {
if (!title.mute) {
c.start();
}
}
}.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
我使用这一行 Sound.call1.play()
来播放声音。谁能帮我解决这个问题?
最佳答案
尝试从头开始寻找剪辑。
c.setFramePosition(0);
c.start();
否则,您第二次调用 start 时将无法播放,因为 Clip 已结束。
关于Java Sound播放一次然后不再播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27583006/