I am working on a program that will have a music component. Basically when certain buttons are pressed I want to play specific song files. I have been looking a lot into playing sound in Java but nothing has worked for me yet. I am currently playing around with some code I found in a tutorial however I am not sure how to specify the file.
I keep getting a FileNotFoundExecption
so I'm obviously referencing the file incorrectly. I have the .wav
file on my desktop and I also have it in a resource source folder in my project. A part of the code is below, any ideas on how I reference the file?
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();
}