This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);

}

}
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Evacuate the building! Here, take this tiny ad with you:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic