0
我使用Java声音用下面的代码:有没有办法让JavaSound使用你的SO上安装的编解码器?
public static void main(String[] args) throws Exception
{
JFrame frame = new JFrame();
frame.setSize(200,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JFileChooser fc = new JFileChooser();
fc.showOpenDialog(null);
File f = fc.getSelectedFile();
AudioInputStream ais = AudioSystem.getAudioInputStream(f);
AudioFormat format = ais.getFormat();
AudioFormat decodedFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED, // Encoding to use
format.getSampleRate(), // sample rate (same as base format)
16, // sample size in bits (thx to Javazoom)
format.getChannels(), // # of Channels
format.getChannels()*2, // Frame Size
format.getSampleRate(), // Frame Rate
false // Big Endian
);
SourceDataLine line = AudioSystem.getSourceDataLine(decodedFormat);
AudioInputStream dais = AudioSystem.getAudioInputStream(decodedFormat, ais);
line.open(decodedFormat);
line.start();
byte[] b = new byte[1024];
int i=0;
while(true)
{
i = dais.read(b, 0, b.length);
if(i == -1)
break;
line.write(b, 0, i);
}
line.drain();
line.stop();
line.close();
ais.close();
dais.close();
}
但播放MP3,tihs要求我有一个SPI对我的类路径...它的确定,altough我一直在寻找一种方式来使用编解码器安装在SO中。 有没有办法做到这一点?
我明白了......但您并未使用SO的编解码器。你像我一样在你的类路径中使用SPI。 我试图使它与已经安装的SO编解码器一起工作 – fredcrs 2010-08-13 11:41:01