我正在尝试使用JLayer为我的游戏添加背景音乐。 如何将其设置为循环播放?
http://www.javazoom.net/javalayer/javalayer.html
BackgroundMusic bm = new BackgroundMusic("music.mp3");
bm.start();
答案 0 :(得分:2)
我有这个方法来播放我的mp3文件
jlayer.jar
mp3plugin.jar
jaudiotagger-2.0.1.jar
private void playMe(){
try {
File file=new File("F:\\Net Beans Work Space\\mp3\\a.mp3");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new Player(bis);
int d=0;
AudioFile audioFile = AudioFileIO.read(file);
d = audioFile.getAudioHeader().getTrackLength();
System.out.print("ddd= "+d) ;
player.play();
} catch(Exception e){
System.out.print("ERROR "+e);
}
}
你需要的是使用线程和jlayer.jar和mp3plugin.jar加上jaudiotagger.jar然后你可以播放mp3文件作为你的背景音乐
答案 1 :(得分:2)
我有另一种在背景播放音频文件的方法,但这种方法是播放.wav声音很容易你可以看一个想法,你可以使用更好的线程来获得出色的背景播放性能
private void playAudioForMenuDropDown(){
String gongFile = "E://Net Beans Work Space//My Computing Fellow//src //computingsounds//menu drop down.wav";
InputStream in = null;
try {
in = new FileInputStream(gongFile);
} catch (FileNotFoundException ex) {
Logger.getLogger(ComputingFellowJobs.class.getName()).log(Level.SEVERE, null, ex);
}
// create an audiostream from the inputstream
AudioStream audioStream = null;
try {
audioStream = new AudioStream(in);
} catch (IOException ex) {
Logger.getLogger(ComputingFellowJobs.class.getName()).log(Level.SEVERE, null, ex);
}
// play the audio clip with the audioplayer class
AudioPlayer.player.start(audioStream);
}