我想在Java中连续播放.wav文件。我发现了一些代码,但我无法使其正常工作。
String fileName = "res/sound/buz.wav";
File file = new File(fileName);
AudioInputStream ais;
try {
Clip clip = AudioSystem.getClip();
ais = AudioSystem.getAudioInputStream(file);
clip.open(ais);
clip.loop(Clip.LOOP_CONTINUOUSLY);
} catch (UnsupportedAudioFileException | IOException
| LineUnavailableException e) {
e.printStackTrace();
}
我在clip.open(ais)上得到了一个无效的格式异常:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid format
at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.createStream(PulseAudioDataLine.java:142)
at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:99)
at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:283)
at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:402)
at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:453)
at launcher.Launcher.main(Launcher.java:59)
我检查过并且文件已正确创建并存在。那么,我的代码有什么问题呢?
如果重要,我正在使用Linux,但这应该适用于Linux和Windows ......