所以我得到了这段为RPG游戏播放音乐的代码。第一种方法播放战斗音乐,另一种方法是在获胜时大张旗鼓。我的问题是音乐堆叠在一起,最终它是一千种不同的战斗音乐和大张旗鼓的播放。当战斗结束并且应该大张旗鼓时,我如何停止音乐,反之亦然?
代码如下:
import java.io.File;
import javax.media.Format;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.PlugInManager;
import javax.media.format.AudioFormat;
public class PlayMusic {
public void playBattle() {
try{
Player player = Manager.createPlayer(new MediaLocator(new File("battle.wav").toURI().toURL()));
player.start();
}
catch(Exception ex){
ex.printStackTrace();
}
}
public void playWin() {
try{
Player player = Manager.createPlayer(new MediaLocator(new File("fanfare.wav").toURI().toURL()));
player.start();
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
import java.io.File;
import javax.media.Format;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.PlugInManager;
import javax.media.format.AudioFormat;
public class PlayMusic {
Player battlePlayer = Manager.createPlayer(new MediaLocator(new File("battle.wav").toURI().toURL()));
Player winPlayer = Manager.createPlayer(new MediaLocator(new File("fanfare.wav").toURI().toURL()));
public void playBattle() {
try{
battlePlayer.start();
}
catch(Exception ex){
ex.printStackTrace();
}
}
public void playWin() {
try{
winPlayer.start();
}
catch(Exception ex){
ex.printStackTrace();
}
}
public void stopWin() {
try{
winPlayer.stop();
}
catch(Exception ex){
ex.printStackTrace();
}
}
public void stopBattle() {
try{
battlePlayer.stop();
}
catch(Exception ex){
ex.printStackTrace();
}
}
}