I'm coding this in java and put my sounds
folder with the bgmusic.wav
in there but it doesn't seem to still identify it. Am I doing something wrong?
Here is my Sound
class:
import java.applet.Applet;
import java.applet.AudioClip;
import java.io.File;
import java.net.MalformedURLException;
public class Sound {
private AudioClip myClip;
public Sound(String fileName) {
try {
File file = new File(fileName);
if (file.exists()) {
myClip = (AudioClip) Applet.newAudioClip(file.toURI().toURL());
} else {
throw new RuntimeException("Sound: file not found: " + fileName);
}
} catch (MalformedURLException e) {
throw new RuntimeException("Sound: malformed URL: " + e);
}
}
public void play() {
myClip.play();
}
}
This is then ran as an Object in my other class to run the bgmusic.wav
:
String fileName = "bgmusic.wav";
String soundDir = "." + File.separator + "sounds" + File.separator;
String filePath = soundDir + fileName;
Sound bgMusic = new Sound(filePath);
bgMusic.play();
If I'm not mistaken I think I have everything setup correctly but I get a Sound: file not found: ./sounds/bgmusic.wav
which is returned from to throw
exception in my Sound
class. Since I have the sounds
folder as a subdirectory of the main java folder passing ./sounds/bgmusic.wav
should be correct right?