我正在尝试实现当我成为我的程序时开始的背景音乐并继续循环当我从 netbeans 运行程序时它工作得很好,但是当我运行 jar 文件时我无法让它工作。任何想法如何使它与我当前的实现一起工作?谢谢
在主要方法中,我称之为:
Sound sound = new Sound();
if (sound.setSong("resources\\music.wav")) { sound.start(); }
这是声音类:
public class Sound extends Thread {
private AudioStream as;
private AudioPlayer p;
private boolean playback;
public void run() { startPlayback(); }
public boolean setSong(String b) {
try {
File a = new File(System.getProperty("user. dir") + "\\src\\" + b);
if (a.exists()) {
// HERE THERE IS THE IMPLEMENTATION WHEN RUN FROM NETBEANS
as = new AudioStream(new FileInputStream(a));
return true;
} else { // HERE SHOULD BE THE JAR FILE IMPLEMENTATION
as = new AudioStream(this.getClass().ge tResourceAsStream(b));
return true;
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Error in loading\n" + ex.getMessage());
}
return false; }
public void startPlayback() {
playback = true;
p.player.start(as);
try {
do { } while (as.available() > 0 && playback);
if (playback) { startPlayback(); }
} catch (IOException ex) {
Logger.getLogger(Sound.class.g etName()).log(Level.SEVERE, null, ex);
}
}
public void stopPlayback() { playback = false; p.player.stop(as); } }