1

I have a game which plays a background music.

I'm using an mp3 library that doesn't support in-jar reading, so I can't use the getClass().getResource() method to find the resource and pass the url to the library, I have to extract that file and pass its path to the library.

I'm using this method to copy the file outside the jar:

File f = new File("15.mp3"); //the music file for output

InputStream is = getClass().getResourceAsStream("15.mp3"); //the mp3 inside the jar
BufferedOutputStream buf = new BufferedOutputStream(new FileOutputStream(f));
byte[] read = new byte[1024];
while (is.read(read) > -1) {
    buf.write(read);
    buf.flush();
}
buf.close();

This method works, the sound gets played, but it's a bit scratchy (the original -which is stored in the jar- is perfect).

Am I doing something wrong with the copy? Am I supposed to do something else to correctly copy an in-jar resource outside the jar package?

BackSlash
  • 21,927
  • 22
  • 96
  • 136

0 Answers0