我有一个需要能够播放声音的游戏.当我点击一个按钮时,声音会播放,但是当我再次点击该按钮时,声音就不会播放.
下面是我的声音类:
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我用这条线Sound.call1.play()来播放声音.任何人都可以帮我解决这个问题吗?
尝试将Clip设置为开头.
c.setFramePosition(0);
c.start();
Run Code Online (Sandbox Code Playgroud)
否则,第二次调用start时无法播放,因为Clip在结尾处.
| 归档时间: |
|
| 查看次数: |
491 次 |
| 最近记录: |