这就是我到目前为止所拥有的一切。但我不确定这是否是播放音频文件的最佳方式,我也想知道如何重复音频文件。有什么建议吗?
package ponggame;
import java.applet.Applet;
import java.applet.AudioClip;
public class Sound {
public static final Sound bgMusic = new Sound("/pong.wav");
public static final Sound hitPaddle = new Sound("/bounce.wav");
private AudioClip clip;
public Sound(String fileName) {
try {
// gets the sound file that is passed in the constructor
clip = Applet.newAudioClip(Sound.class.getResource(fileName));
} catch (Exception e) {
e.printStackTrace();
}
}
// plays the music, obviously
public void play() {
try {
new Thread() { //multi-tasking stuff
public void run(){
clip.play();
}
}.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}