johnywhite
johnywhite

Reputation: 21

sound in Eclipse, but no sound in runnable JAR file

When I run app in Eclipse, sound played well, but if I export app to runnable jar, sound doesn't work.

I searched a lot on the internet, they all use getResourceAsStream, but i didn't find a solution with FileInputStream.

In the jar file progress_bar.wav is located at the root, but even soundFile = "progress_bar.wav" doesn't work.

src
--controller
----FileManager.java
--res
----progress_bar.wav

public void display_L11_errors(final JProgressBar pBar, String... args) throws IOException
{
  String soundFile = "res/progress_bar.wav";
  InputStream in;
  AudioStream audioStream = null;
  try {
        in = new FileInputStream(soundFile);
        audioStream = new AudioStream(in);
        AudioPlayer.player.start(audioStream);
  } catch (IOException e1) {  e1.printStackTrace();  }

  //for loop

  AudioPlayer.player.stop(audioStream);
}

Any one please provide me an answer ?

EDIT: As @greg-449 told me, entries in a Jar are not files so you can't use FileInputStream to access them.

SOLUTION:

AudioStream audioStream = null;
audioStream = new AudioStream(FileManager.class.getResourceAsStream ("/progress_bar.wav"));
AudioPlayer.player.start(audioStream);
//for loop
AudioPlayer.player.stop(audioStream);

Upvotes: 0

Views: 1257

Answers (2)

Bole
Bole

Reputation: 1

  1. Create "Folder Source" in your project. Right click on your project and find folder called like that.

  2. Put your wav file/s in your folder source by dragging. But directly in your IDE.

  3. Compiler your project and save it.

Now, when you open your work IDE folder(save folder), your wav file/s should be to your source folder and bin folder, no other place. Do not put file/s to bin folder. Will be added automatically, when you run your program.

Note: Call your wav file/s like this to your Java project: "Name of wav file.wav";

Path of file/s not needed.

When a your executable jar file is saved, try to remove your wav file/s from source. Check it if works like that. Should be possible to get it works on other devices...

Edit: Maybe sound will not works in IDE, but as saved jar file will it... To works in your IDE, copy wav file/s in workspace folder, but remove them when you want to export...

I using Eclipse, and it's kind a tricky to make it works, but once done it's done. Depends of which library use it...

Upvotes: 0

mcjcloud
mcjcloud

Reputation: 371

Create a separate folder besides your "src" folder, like "res", and keep your file in there, and reference your file with a URL:

URL url = <NameOfClass>.class.getResource("/progress_bar.wav");

Then configure your build path by right clicking the project > build path > configure build path > source > Add Folder > check "res", press okay, and press okay again, then try exporting the project again.

change

in = new FileInputStream(soundFile);

to

in = new FileInputStream(url);

Upvotes: 2

Related Questions