好的,所以我正在制作一个游戏,当你在不同的地区或有中断时,音乐会发生变化,比如人工智能。
所以我刚刚学会了如何在我的程序中显示音乐,现在我正试图让它停止,但我不确定如何,下面是一段音乐播放的代码片段,然后我尝试用新的覆盖它动作发生时的音乐。
public static void songs(String word) {
String temp = word;
if (temp.equals("start")) {
try {
try {
blah = new FileInputStream("C:/Users/Austin/Desktop/Storage/programimages/game/battle.wav");
} catch (Throwable e) {
e.printStackTrace();
}
AudioStream as = new AudioStream(blah);
AudioPlayer.player.start(as);
System.out.println("going");
} catch (IOException e) {
System.err.println(e);
}
}
if (temp.equals("stop")) {
try {
try {
blah = new FileInputStream("C:/Users/Austin/Desktop/Storage/programimages/game/silence.wav");
} catch (Throwable e) {
e.printStackTrace();
}
AudioStream as = new AudioStream(blah);
AudioPlayer.player.stop(as);
System.out.println("stopping");
} catch (IOException e) {
System.err.println(e);
}
}
}
这是我能找到的唯一可以播放音乐的方法,但是如果你们有任何其他建议,请告诉我。
再一次,我想要声音效果和音乐,现在所发生的只是一首歌会播放,它在任何情况下都不会停止,直到它到达长度的尽头。我希望能够在新一首歌曲出现时停止歌曲,并允许声音效果弹出。
谢谢!(因为我被困在这个问题上并且现在需要一个答案,所以我可能会在另外一个或两个 Java 网站上重新发布,以便我能尽快得到回复,不过还是谢谢你!!!)
编辑代码:(仍然不会停止当前流,任何更多建议表示赞赏)
public static void songs(String word) throws IOException {
String temp = word;
if (temp.equals("go")) {
try {
blah = new FileInputStream("C:/Users/Austin/Desktop/Storage/programimages/game/battle.wav");
} catch (Throwable e) {
e.printStackTrace();
}
AudioStream as = new AudioStream(blah);
AudioPlayer.player.start(as);
System.out.println("going");
}
if (temp.equals("stop")) {
//don't try and do things with a null object!
if (as != null) {
AudioPlayer.player.stop(as);
System.out.println("stopping1");
}
System.out.println("stopping2");
AudioPlayer.player.stop(as);
}
}