【发布时间】:2014-12-23 19:28:02
【问题描述】:
我正在开发一个包含音乐组件的程序。基本上,当按下某些按钮时,我想播放特定的歌曲文件。我一直在研究用 Java 播放声音,但对我来说还没有任何效果。我目前正在玩一些我在教程中找到的代码,但是我不确定如何指定文件。
我不断收到FileNotFoundExecption,所以我显然错误地引用了该文件。我的桌面上有.wav 文件,我的项目的资源源文件夹中也有它。部分代码如下,关于我如何引用文件的任何想法?
public static void main(String[] args) throws Exception {
// specify the sound to play
// (assuming the sound can be played by the audio system)
File soundFile = new File("/desktop/14_Wonderland.wav");
AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);
// load the sound into memory (a Clip)
DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(sound);
// due to bug in Java Sound, explicitly exit the VM when
// the sound has stopped.
clip.addLineListener(new LineListener() {
public void update(LineEvent event) {
if (event.getType() == LineEvent.Type.STOP) {
event.getLine().close();
System.exit(0);
}
}
});
// play the sound clip
clip.start();
}
【问题讨论】: