那么我如何修复它:
我基本上遵循了 Charles Goodwin 的提示,但我更改了 AePlayWave 类以实现可运行并将其与线程池一起使用(以避免一直启动新线程的开销)。此外,我使用 URL 而不是文件来使用打包 JAR 文件中的资源。AePlayWave 对象在设置完成(选择文件)或设置更改时创建。我希望应用程序播放的每个声音都有一个实例。事件的侦听器方法然后触发池为该事件声音运行特定的 AePlayWave 实例。其余基本相同。
只有两个不方便的问题:
1.) 在弱机器上,WAV 的结尾并不总是播放。当声音很短(比如 100 毫秒的哔声)时,这可能会导致根本没有声音播放!这就是为什么我在我想播放的每个声音的末尾添加了 500 毫秒的静音。这是一种解决方法,但它有帮助,目前它似乎是最好和最稳定的方法。
2.) 如果播放多个声音(由于非常快速的重复),声音会重叠,您会听到音调和音量发生变化。在我的情况下这没问题,但对于其他用途可能会很烦人。
它已经在生产系统上运行。如果向我报告任何错误,我将编辑此帖子以使您保持最新状态。
现在这里是(基本上减少的)源代码:
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class AudibleListener implements SomeListener {
private Runnable successRunner;
private Runnable failRunner;
ExecutorService pool = Executors.newCachedThreadPool();
/**
* Call this after initialization and after every change in your config at runtime.
*/
public void reloadSettings() {
// put your configuration here
this.successRunner = new WavePlayer(this.getClass().getResource("success.wav"));
this.failRunner = new WavePlayer(this.getClass().getResource("fail.wav"));
}
/**
* Call this to savely shutdown the thread pool.
*/
public void shutdown() {
this.pool.shutdown();
}
/**
* Listener method called on success.
*/
public void eventSuccess() {
this.pool.execute(this.successRunner);
}
/**
* Listener method called on fail.
*/
public void eventFailed() {
this.pool.execute(this.failRunner);
}
private class WavePlayer implements Runnable {
private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb
private URL soundFile;
public WavePlayer(URL soundFile) {
this.soundFile = soundFile;
}
@Override
public void run() {
try {
// check if the URL is still accessible!
this.soundFile.openConnection().connect();
this.soundFile.openStream().close();
} catch (Exception e) {
return;
}
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem
.getAudioInputStream(this.soundFile);
} catch (UnsupportedAudioFileException e) {
return;
} catch (IOException e) {
return;
}
AudioFormat format = audioInputStream.getFormat();
SourceDataLine auline = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
} catch (LineUnavailableException e) {
return;
} catch (Exception e) {
return;
}
auline.start();
int nBytesRead = 0;
byte[] abData = new byte[this.EXTERNAL_BUFFER_SIZE];
try {
while (nBytesRead != -1) {
nBytesRead = audioInputStream
.read(abData, 0, abData.length);
if (nBytesRead >= 0) {
auline.write(abData, 0, nBytesRead);
}
}
} catch (IOException e) {
return;
} finally {
auline.drain();
auline.close();
}
}
}
}
欢呼并感谢到目前为止的所有帮助!
P。
更新:
这现在运行了过去 72 小时,没有任何错误!看起来我们成功了!