Java-游戏背景歌曲(如何添加)
我正在尝试创建一个游戏,目前我正在尝试为其添加声音,但由于缺乏经验,我未能做到这一点,所以我请求您的帮助。Java-游戏背景歌曲(如何添加),java,applet,audio,Java,Applet,Audio,我正在尝试创建一个游戏,目前我正在尝试为其添加声音,但由于缺乏经验,我未能做到这一点,所以我请求您的帮助。 如何将背景歌曲添加到游戏中?启动单独的播放声音线程。在这个线程中播放它。这样,您的游戏将运行,同时您可以运行声音 public static synchronized void playSound(final String url) { new Thread(new Runnable() { // The wrapper thread is unnecessary, unless
如何将背景歌曲添加到游戏中?启动单独的播放声音线程。在这个线程中播放它。这样,您的游戏将运行,同时您可以运行声音
public static synchronized void playSound(final String url) {
new Thread(new Runnable() {
// The wrapper thread is unnecessary, unless it blocks on the
// Clip finishing; see comments.
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(
Main.class.getResourceAsStream("/path/to/sounds/" + url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
}
当游戏或普通背景音乐中发生随机播放或循环播放时,您希望触发某些声音吗?@user2860598:如果您不介意,两者都可以。谢谢,我确实导入了这些声音,但我听不到声音。
public static void music() {
AudioPlayer MGP = AudioPlayer.player;
AudioStream BGM;
AudioData MD;
ContinuousAudioDataStream loop = null;
try {
BGM = new AudioStream(new FileInputStream("som.wav"));
MD = BGM.getData();
loop = new ContinuousAudioDataStream(MD);
} catch(IOException error) {
System.out.println("Error!!!");
}
MGP.start(loop);
}