The following code is used for playing a sound file in my java applet:
public synchronized void play() { try { //Here filename is a URL retreived through //getClassLoader().getResource() InputStream in = new FileInputStream(filename.getFile()); AudioStream as = new AudioStream(in); AudioPlayer.player.start(as); } catch (IOException e) { e.printStackTrace(); }
It works when i run the applet locally using Eclipse, but if I try packaging it in a .jar and running it as an applet in the web browser, it doesn’t work. Commenting out this code makes the applet work.
What shall I substitute the above code with so it’ll work in the applet?
Try using getResourceAsStream() on the ClassLoader instead of new FileInputStream(). This will return an InputStream that you can pass to the AudioStream. So something like: