The play method below is from a class which, upon instantiation, reads a .wav file into a byte array called data, and stores the sound format in an AudioFormat object called format.
I have a program that calls play from a java.util.Timer. When I go into the folder with all the relevant .class files and I run the program using the command java MainClass, everything works as expected. However, when I put all the .class files in an executable .jar and run the program using the command java -jar MyProgram.jar, sounds played using the play method are cut off after something like 50 to 150 ms.
public void play() throws LineUnavailableException {
final Clip clip = (Clip)AudioSystem.getLine(new DataLine.Info(Clip.class, format));
clip.open(format, data, 0, data.length);
new Thread() {
public void run() {
clip.start();
try {
Thread.sleep(300); // all sounds are less than 300 ms long
} catch (InterruptedException ex) { /* i know, i know... */ }
clip.close();
}
}.start();
}
A few comments:
-
I’ve tried increasing the sleep time in the
playmethod up to 1000 ms, with no change in behavior. -
Timing the
Thread.sleepusingSystem.nanoTimeconfirms that the thread is sleeping exactly as long as expected. -
Since the sound file to be played is pre-loaded into memory, I don’t think the act of extracting the sound resource from the .jar can be causing the problem.
-
I’ve tried running the program from both inside and outside the jar using the memory pool size options
-Xms2mand-Xmx64m(separately), with no change in behavior.
I’m running OpenJDK Java 6 on Ubuntu 11.04. Any idea what’s going on?
It is likely the loading of the
byte[]that is the problem.