• Post Reply Bookmark Topic Watch Topic
  • New Topic

Playing wav - java.io.FileNotFoundException when file is def there  RSS feed

 
Paul Carter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);

}

}
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are compiler errors. They have nothing to do with the existence or not of the .wav file. Have a read through the Java Tutorial on Exceptions.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, if a method can throw an exception, it has to either:

1) be surrounded by a try block

or

2) the method must declare that it throws an exception
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are compiler errors.

That means that your program never executed, and therefore the program never attempted to look for the file. Instead, when you tried to compile your program, there was something technically wrong with your code, i.e. your code did not follow the rules of the Java language.

Since it's possible that when your code does excecute, the file will not be found, the Java language requires that your program has a way of dealing with that circumstance.
[ October 04, 2006: Message edited by: sven studde ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!