0

I'm trying to stream a .wav file. I'm getting this error:

java.io.FileNotFoundException: \sounds\player-bullet.wav (The system cannot find the file specified)

The file structure is src/sounds/player-bullet.wav. The java file is in src. This is the code:

AudioInputStream ais = AudioSystem.getAudioInputStream(new  File("/sounds/player-bullet.wav"));

But in the same file I load an image with this code and it works:

shipImage = new Image("/images/player.png");

The file structure is src/images/player.png. This code works in eclipse:

AudioInputStream ais = AudioSystem.getAudioInputStream(new File("./src/sounds/player-bullet.wav"));

But when I make it into a jar file, the src file isnt included. So this won't work when the file is in a jar.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
Flompy Doo
  • 21
  • 9
  • You marked your question as JavaFX, but it does not appear to be JavaFX related. Does your application extend [JavaFX Application](https://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html)? If not, it is not a JavaFX Application and the method to play a sound differs from a JavaFX [MediaPlayer](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/media/MediaPlayer.html) or [AudioClip](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/media/AudioClip.html). (I haven't used the Java sound `AudioSystem`, so I can't comment on that). – jewelsea Mar 07 '17 at 23:04
  • it does extend Javafx – Flompy Doo Mar 07 '17 at 23:08
  • I edited the tags to remove the JavaFX reference. You probably want to use [`getResourceAsStream()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResourceAsStream-java.lang.String-), you probably don't need a File object. The question is probably a duplicate of: [File loading by getClass().getResource()](http://stackoverflow.com/questions/14089146/file-loading-by-getclass-getresource). – jewelsea Mar 07 '17 at 23:15
  • the method i use plays sounds, it just wont find the file what its a jar file. – Flompy Doo Mar 07 '17 at 23:23
  • That's because it is not really a file it is a resource inside a jar file. That is why you don't use `File`, instead just use `getResourceAsStream()` as suggested. For more info read: [Java resource loading explained](http://www.thinkplexx.com/learn/howto/java/system/java-resource-loading-explained-absolute-and-relative-names-difference-between-classloader-and-class-resource-loading). Check that the file is actually in the jar file too at the location you expect (via `jar tvf .jar`). – jewelsea Mar 07 '17 at 23:43
  • It would throw UnsupportedAudioFileExeption, and why would it work in ecplise and not in a jar – Flompy Doo Mar 08 '17 at 01:07
  • I don't know, I don't use the `AudioSystem` perhaps the issue is specific to that. The information I provided in the comments is just general information on how to access resources from a jar file. – jewelsea Mar 08 '17 at 18:01

0 Answers0