Paul's SoundSystem, set folder for files

So, I want to keep my sound-files outside of my jar, in a folder res.

Paul allows one to choose a package inside the Src folder, like this:


SoundSystemConfig.setSoundFilesPackage("Sound");

But I can’t get outside of the src folder!

Does anyone know how to do this?

Oh, and by the way, I’ve fixed Pauls LWJGL-jar so that it runs with LWJGL 3.0 if anyone is interested!

I’m not familiar with the library, but according to the documentation there’s a loadSound() function that takes a URL. Have you tried this with a URL representing a path to one of your files? Assuming the library is open-source, perhaps you could also find the location in the code where the loading occurs and see if accessing files outside the JAR is supported. (Or, perhaps you could just ask the developer directly.)

Also, may I ask why you want to keep your resources outside the JAR? I’m not saying you shouldn’t, but knowing what your goals are might help. (My understanding is that loading resources from JARs is generally preferable though - for one thing, it sidesteps some potential issues with the location of the current working directory and/or acquiring the absolute path to your external resources folder.)

In the javadoc he says that URL’s are used only to load resources from the internet. I am quite unfamiliar with the use of URL’s. Don’t know exactly what they are… But I could try.

My res folder is still in the project folder, so I can use relative paths I think. I like to keep it this way, since my game is getting rather big and the src folder is cluttered with packages as it is and I like to keep a distinction between files and code.

How would one go about using an URL for this purpose?

I’ve got the following folder structure:

Game
src
res
sound

Contact Paul, send him a nice patch ;D

There are two different forms for locating folders for a project, depending on whether you start the location with a “/” or not. Using the “/” is an absolute address that starts from the code base, otherwise the location is relative to the location of the class that is used for the call. I’m assuming the form getResource(filename) or getResourceAsStream(filename).

The symbol “…” is useful for backing up one level.

In general URL’s are more reliable, since the file system is unable to locate files embedded in a jar. URL = Uniform Resource Locator. The JavaDocs for URL has more info.

If you have a Class called PlaySound in folder src, and res is a “neighboring” folder (as in your diagram), you should be able to use the following.


URL url = PlaySound.class.getResource("../res/sound");

I did a test where my structure was the following:

jgoQuestions // a package in the Eclipse project
res
foo.txt
src
ResourceTest

From here, the following worked (running the class ResourceTest):

URL url = ResourceTest.class.getResource("../res/foo.txt");  // relative form
URL url = ResourceTest.class.getResource("/jgoQuestions/res/foo.txt");  //absolute form

Thanks!

I put all your advice together and solved it this way:

this is the structure:

folder MyGame
src
Sound.java
res
sound
shout.wav

in Sound.java:


String path = "res/sound/shout.wav";
URL url = new File(path).toURI().toURL;

SoundSystem.newSource(URL....);

Don’t know if this is bad practise or not, but it works!

If I’m not mistaken (and I may be), that solution relies on the current working directory being your game folder, which may or may not be the case depending on how and from where your app was run. Whether this matters depends, I suppose, on how you intend to distribute the app, but in my experience the working directory may end up elsewhere in some deployment scenarios. Anyway, just something to keep in mind.