我有以下方法来播放音频剪辑:
public static synchronized void playSound(final String path) {
new Thread(new Runnable() {
// The wrapper thread is unnecessary, unless it blocks on the
// Clip finishing; see comments.
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(
this.getClass().getClassLoader().getResourceAsStream(path));
clip.open(inputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);
while (runThread) {
Thread.sleep(5000);
}
clip.stop();
}
catch (Exception e) {
System.err.println("Audio clip error: "+e.getMessage());
e.printStackTrace();
}
}
}).start();
}
当我在 Windows 7 桌面平台(联想)上运行它时,它按预期工作。但是,在 Windows 7 笔记本电脑(Acer)上运行时,出现以下异常:
java.lang.NullPointerException
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at StokerMonitor.Alerts$1.run(Alerts.java:62)
at java.lang.Thread.run(Unknown Source)
第 62 行是“AudioInputStream”语句。我不知道这两个平台之间有什么区别,但我猜笔记本电脑上肯定缺少一些 Windows 7 依赖项。有没有人有什么建议?TIA。