我正在尝试播放来自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放在类路径中。我和this sample ogg from Wikipedia试过了。但是它仍然不工作,它给了我一个UnsupportedAudioFileException。
你知道我可能做错了什么吗?
发布于 2012-12-10 15:52:45
哦..。原来我的类路径中并没有所有的依赖项: tritonus_share.jar、jorbis-0.0.15.jar、jogg-0.0.7.jar。
https://stackoverflow.com/questions/13796057
复制相似问题