我正在尝试从 Java 程序播放 OGG Vorbis 文件。PCM 文件 (*.wav) 与此代码一起工作正常:
public void play(String resFile) throws Exception {
AudioInputStream audioInputStream = null;
URL audioSource = new File(resFile).toURL();
audioInputStream = AudioSystem.getAudioInputStream(audioSource);
AudioFormat format = audioInputStream.getFormat();
SourceDataLine line = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format);
line.start();
int bytesRead = 0;
byte[] data = new byte[32 * 1024];
try {
audioInputStream.mark(Integer.MAX_VALUE);
while(bytesRead != -1) {
bytesRead = audioInputStream.read(data, 0, data.length);
if(bytesRead >= 0) {
line.write(data, 0, bytesRead);
}
Thread.yield();
}
} finally {
line.drain();
line.close();
}
}
为了也能播放 OGG 文件,我下载了Vorbis SPI并将 jar 放在类路径中。我尝试使用来自 Wikipedia 的示例 ogg。但它仍然不起作用,它给了我一个 UnsupportedAudioFileException。
你知道我做错了什么吗?