Exporting Jar with OpenAL

I have a exported Jar that loads WAV files through this method:

public void loadSound(String path) throws FileNotFoundException {
		WaveData data = null;
		data = WaveData.create(GameSound.class.getClassLoader().getResourceAsStream(path));
		int buffer = alGenBuffers();
		alBufferData(buffer, data.format, data.data, data.samplerate);
		data.dispose();
		source = alGenSources();
		alSourcei(source, AL_BUFFER, buffer);
	}

The sound files I’m trying to load are in the same package as the class that the above method is in. I’ve tried to use all sorts of ways to load the WaveData, but none of them work after exporting; they all return a NPE or file not found. Has anyone here used OpenAL? How do I get the files in the same package after exporting? I guess this is a newbie issue because it is just file handling, but I never took the time to learn the difference between a lot of the Java IO functions…


Well, I tried this:


public void loadSound(String path) {
		InputStream is = GameSound.class.getResourceAsStream("sound" + File.separatorChar + path);
		System.out.format("is " + path + "  null ? => %s", is == null);
		System.out.println();
		WaveData data = WaveData.create(is);
		int buffer = alGenBuffers();
		alBufferData(buffer, data.format, data.data, data.samplerate);
		data.dispose();
		source = alGenSources();
		alSourcei(source, AL_BUFFER, buffer);
	}

Again, it works fine in Eclipse but not after exporting.

Edit:
Tried this way now, still doesn’t work. I guess this thread is just going to turn into a personal log of failure…


public void loadSound(String path) {
		URL is = GameSound.class.getResource("sound" + File.separatorChar + path);
		System.out.println(is.getPath());
		System.out.format("is " + path + "  null ? => %s", is == null);
		System.out.println();
		WaveData data = WaveData.create(is);
		int buffer = alGenBuffers();
		alBufferData(buffer, data.format, data.data, data.samplerate);
		data.dispose();
		source = alGenSources();
		alSourcei(source, AL_BUFFER, buffer);
	}

Edit: With this code:


public void loadSound(String path) {
		InputStream is = GameSound.class.getClassLoader().getResourceAsStream("com" + File.separatorChar + "nishu" + File.separatorChar + "ld28 "  + File.separatorChar + "utilities" + File.separatorChar + "sound" + File.separatorChar + path);
		WaveData data = WaveData.create(is);
		int buffer = alGenBuffers();
		alBufferData(buffer, data.format, data.data, data.samplerate);
		data.dispose();
		source = alGenSources();
		alSourcei(source, AL_BUFFER, buffer);
	}

I get a NPE with this line:

		alBufferData(buffer, data.format, data.data, data.samplerate);

Maybe I’m getting somewhere? I don’t think so though.


I know I’m being annoying but this is for Ludum Dare which ends in just a few hours and I just can’t get OpenAL to load the damn files… I have tried countless things and now I really need help.

Well, I got it to work. I had to put all the classes and music files in the default package. Now though, it doesn’t load two of my sound files. It just says the system cannot find the file specified in the exported Jar, but it works in Eclipse. grr.

Class loader issue my friend

dont’ blame on OpneAL

all the issue is related to this line of code
GameSound.class.getResourceAsStream(“sound” + File.separatorChar + path);

question what is the class loader that load the class GameSound?
for sure is not the same where is loaded the resource “sound” + File.separatorChar + path

it works in eclipse because it load all the resources of the project in the same classpath

and what you mean to export the jar?
are you executing the main class with command line?
if you will run the class by command line and will add with option -cp or -classpath all the jar, it will works

this is an example
java -cp …/lib/application.jar;…/other.jar;…/config com.x.y.z.MainClass

./config is a folder if you wont to add file as images configuration file wave file etc directly in file system withtou bind in a jar

i suppose that you create an executable jar correct?

another important point is the definition of the resource name that is different if call in the
getResourceAsStream in the ClassLoader or Class

this is the correct sintax (look the char / based on object that call the method getResourceAsStream)
ClassLoader.getResourceAsStream(“some/pkg/resource.properties”);
Class.getResourceAsStream ("/some/pkg/resource.properties");

I figured it out, don’t mind me. But I still wonder why some sounds don’t play… Oddly enough they were my two background sounds. They were perfectly fine in Eclipse but when I exported the Jar they stopped playing. Does OpenAL have a size limit? Because the one file I was trying to play was 14 MB. The other file was only 4 MB, so I don’t think that’s the issue.

Another problem I found was that sounds wouldn’t play exactly when I called them to start, which really annoyed me. For instance, when the player dropped to a certain health level I call on a heartbeat sound to start playing. It doesn’t play until I pause the game though, and then stops when I unpause it. I had other sounds playing at the other time, so maybe that was the issue? Either way its kind of annoying that a sound library just doesn’t work as its supposed to…

regarding this point if it works in eclipse it works outside eclipse, i mean if size limit exist, exist inside and outside!
so again i ask you an important question, what you mean for
" I exported the Jar " ?
can you explain how i structured your deployment version of your project in the way that may be will be more easy help you to find a solution and explain why it is not working outside eclipse?

did you try to start the project with command line?
do you have the same behavior?

this instead is related to another issue,
from you description look like that the thread that manage the sound start only when the thread that manage the game is stopped
may be in this case try to attach code to understand whats is happening
(anyway for this other question may be is better to create a different thread in the forum)