Почему Java Sound ведет себя по-другому, когда я запускаю из .jar?
play
Метод ниже из класса, который при создании экземпляра читает WAV-файл в байтовый массив с именемdata
и сохраняет звуковой формат вAudioFormat
объект называетсяformat
.
У меня есть программа, которая вызываетplay
изjava.util.Timer
, Когда я захожу в папку со всеми соответствующими файлами .class и запускаю программу с помощью командыjava MainClass
все работает как положено. Однако, когда я помещаю все файлы .class в исполняемый файл .jar и запускаю программу с помощью командыjava -jar MyProgram.jar
звуки воспроизводятся с использованиемplay
метод обрезается примерно через 50-150 мс.
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();
}
Несколько комментариев:
I've tried increasing the sleep time in the play
method up to 1000 ms, with no change in behavior.
Timing the Thread.sleep
using System.nanoTime
confirms 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 -Xms2m
and -Xmx64m
(separately), with no change in behavior.
Я использую OpenJDK Java 6 в Ubuntu 11.04. Есть идеи, что происходит?