Trying to play a sound in
Java but having huge difficulty getting code samples to run when they're pasted into Netbeans 5.0.
So, got a short snippet of code from what looks like a reliable source
(
http://www.javaworld.com/javaworld/javatips/jw-javatip24.html)
and pasted it into a fresh app.
Getting 2 errors as follows:
1. unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
InputStream in = new FileInputStream("C:/battle.wav");
2. unreported exception java.io.IOException; must be caught or declared to be thrown
AudioStream as = new AudioStream(in);
and I can assure you that the file C:\battle.wav does exist
Any ideas?
package sounddemo;
import sun.audio.*; //import the sun.audio package
import java.io.*;
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(
String[] args) {
// TODO code application logic here
//** add this into your application code as appropriate
// Open an input stream to the audio file.
InputStream in = new FileInputStream("C:/battle.wav");
// Create an AudioStream object from the input stream.
AudioStream as = new AudioStream(in);
// Use the static class member "player" from class AudioPlayer to play
// clip.
AudioPlayer.player.start(as);
// Similarly, to stop the audio.
AudioPlayer.player.stop(as);
}
}