1.导入jar包
2.要用线程来控制播放,要不然窗体就要等歌曲播放完才能出现。
//主背景音乐。
class MusicPlayer extends Thread{
Player player;
String music;
public MusicPlayer(String file) {
this.music = file;
}
public void run() {
try {
play();
} catch (FileNotFoundException | JavaLayerException e) {
e.printStackTrace();
}
}
public void play() throws FileNotFoundException, JavaLayerException {
BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(music));
player = new Player(buffer);
player.play();
}
}
//主背景音乐开启。
MusicPlayer music=new MusicPlayer("musics\\02.mp3");
music.start();