jQuery in Action, 3rd edition
The moose likes Game Development and the fly likes audio resources for Applet, alternatives to Jar? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "audio resources for Applet, alternatives to Jar?" Watch "audio resources for Applet, alternatives to Jar?" New topic

audio resources for Applet, alternatives to Jar?

Phil Freihofner
Ranch Hand

Joined: Sep 01, 2010
Posts: 115
I have some wavs that I want to play via an JApplet. I am using javax.sound.sample. The wav files play fine when included in the jar. But they are large, and make the load time for the JApplet rather long. (With audio, start takes 3 minutes, without audio, start takes 10 seconds!)

A friend suggested that there might be a way to store and access the audio files on the server rather than include them in the jar. I've tried putting the audio files in a sub directory of the codebase, and various ways to input the files, but am not having any luck.

This works, when the wav files are part of the jar:
In this case, the audio files are in a subdirectory off of the a package where I have the playback code.

gongWav is a string, such as "a4.wav". An example can be heard here:
I tried using the same code with an absolute address for the audio, as in:

I also tried loading as a stream from a URL like this:

In both cases, the result was silence.

Part of the difficulty here is that I don't have a lot of technique for debugging Applets. It kind of has to be done at that location, as the problem is location-specific. Normally, I use Eclipse and a lot of System.out.println() statements, and that suffices, or occasionally I'll run the debugger. But I don't see how that can help me here. I'm not even clear how one finds the System.out.println() output when running code from the Browser.

If you'd like to see/hear the app, it is a puzzle game in progress, my first 2D game attempt with Java, and there is a lot to do with it still. Like, include directions as to how to play!
www.hexara.com/game.html (takes about 3 minutes to load, but has audio within the jar)
www.hexara.com/gameNS.html (NS=no sound, loads in about 10 seconds)
If you are intrigued and want instructions, I will be happy to provide them.

Anyway, it would be pretty awesome if there was a way to use my bulky sound files in the program without affecting the start time so severely. I'm trying to avoid using MP3 or other lossy compression formats.

Many thanks!
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
URL audioWav = new URL("http://www.hexara.com/Audio/" +gongWav);
InputStream currentIS = audioWav.openStream();

This should work, provided the URL is correct. Do you see the HTTP request in the server's access logs? What result code does it produce?

Another option might be to download the wav files in a background thread started when the applet initializes. That way the applet GUI can start independently of any downloading. For that to work the wav files should be stored as loose file on the server, not as part of a jar file.
Phil Freihofner
Ranch Hand

Joined: Sep 01, 2010
Posts: 115
Thank you for the reply and the suggestions!

I don't know whether I have access to server logs on this server, nor how to inspect them! With your assurance the method is correct, I think I will try again when I am fresher, and confirm I didn't make a typo or leave a flag in a wrong state when I made the jar. For example, maybe a missed capitalization.

[Back again--it turns out this method that you said should work, does work, after a fashion. Since you said it should work, I decided to just try running it again. And yes, if one waits a few minutes, the sounds DO start to play, but the performance is exceedingly choppy. I guess the JVM in the browser is attempting, by this command form, to get the sound files from the web site for each play, and that this is very slow.]
I agree. Here's the link: http://aspose.com/file-tools
subject: audio resources for Applet, alternatives to Jar?
jQuery in Action, 3rd edition