我有一个需要能够播放声音的游戏。当我点击一个按钮时会播放声音,但是当我再次点击该按钮时,声音就不会播放。
以下是我的声音课程:
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()
播放声音。任何人都可以帮我解决这个问题吗?
答案 0 :(得分:5)
尝试将剪辑设置为开头。
c.setFramePosition(0);
c.start();
否则,第二次调用start时无法播放,因为Clip已结束。