File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Playing wav - java.io.FileNotFoundException when file is def there Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Playing wav - java.io.FileNotFoundException when file is def there" Watch "Playing wav - java.io.FileNotFoundException when file is def there" New topic
Author

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

Paul Carter
Ranch Hand

Joined: Sep 20, 2006
Posts: 57
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

Joined: Oct 29, 2001
Posts: 8259

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
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

Joined: Sep 26, 2006
Posts: 148
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Playing wav - java.io.FileNotFoundException when file is def there
 
Similar Threads
Sound problem in Java
How do I loop the sound file in this program>
Running audio files through java application
I need HELP on Java Sound!!!
try without catch