我想在GUI触发的操作成功和错误时播放简短的蜂鸣声(WAV文件)。
我偶然发现了javax.sound.sampled.Clip,它似乎起作用了。下面是我使用的基本代码:
clip.stop();
clip.setFramePosition(0);
clip.start();这是在单击按钮触发数据库操作后执行的。在成功和错误时,将播放两个不同的预加载剪辑。
但是在生产机器(运行Kubuntu10.4的旧PC )上,经过一段时间(大约400+执行或2-4个小时),剪辑拒绝播放。stop方法大约需要3秒才能终止,并且下面的start操作不会播放任何声音。随后的每一次代码调用都会失败,并且不会抛出异常或任何其他反馈。唯一能解决这个问题的就是重新启动整个应用程序。
我的问题是:有什么解决办法吗?其他人也有同样的问题吗?或者有没有其他框架可以用来播放至少两种不同的声音( Toolkit.beep()只能播放一种声音)。
发布于 2011-07-12 23:25:09
所以我是如何修复它的:
我基本上遵循了Charles Goodwin的提示,但我更改了AePlayWave类以实现runnable并将其与线程池一起使用(以避免总是启动新线程的开销)。另外,我使用URL而不是Files来使用打包的JAR文件中的资源。AePlayWave对象是在安装完成(选择文件)或设置更改时创建的。我希望应用程序播放的每个声音都有一个实例。然后,事件的侦听器方法触发池为该事件声音运行特定的AePlayWave实例。其余的基本上是一样的。
只有两个不方便的问题:
1.)在较弱的机器上,WAV的结尾并不总是被播放。当声音非常短(例如100ms的蜂鸣声)时,这可能导致根本没有声音被播放!这就是为什么我在我想要播放的每个声音的末尾添加了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小时,没有任何错误!看起来我们成功了!
发布于 2011-07-09 07:42:47
不要害怕只重新创建对象,开销很低。尝试只创建新的剪辑,而不是重置剪辑。你可以缓存这些文件,这将是一个有用的优化。重用剪辑对象不是这样的。
或者,您可以尝试不受限制的替代实现。
这是谷歌对“java play wav文件”的最高搜索结果:
http://www.anyexample.com/programming/java/java_play_wav_sound_file.xml
它将事情简化为一个单独的调用:
new AePlayWave("test.wav").start();只需将这个类添加到您的代码库:
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class AePlayWave extends Thread {
private String filename;
private Position curPosition;
private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb
enum Position {
LEFT, RIGHT, NORMAL
};
public AePlayWave(String wavfile) {
filename = wavfile;
curPosition = Position.NORMAL;
}
public AePlayWave(String wavfile, Position p) {
filename = wavfile;
curPosition = p;
}
public void run() {
File soundFile = new File(filename);
if (!soundFile.exists()) {
System.err.println("Wave file not found: " + filename);
return;
}
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
} catch (UnsupportedAudioFileException e1) {
e1.printStackTrace();
return;
} catch (IOException e1) {
e1.printStackTrace();
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) {
e.printStackTrace();
return;
} catch (Exception e) {
e.printStackTrace();
return;
}
if (auline.isControlSupported(FloatControl.Type.PAN)) {
FloatControl pan = (FloatControl) auline
.getControl(FloatControl.Type.PAN);
if (curPosition == Position.RIGHT)
pan.setValue(1.0f);
else if (curPosition == Position.LEFT)
pan.setValue(-1.0f);
}
auline.start();
int nBytesRead = 0;
byte[] abData = new byte[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) {
e.printStackTrace();
return;
} finally {
auline.drain();
auline.close();
}
}
} https://stackoverflow.com/questions/6627730
复制相似问题