0

我正在使用带有以下代码的 java Sound:

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……没关系,尽管我一直在寻找一种方法来使用安装在 SO 中的编解码器。有没有办法做到这一点?

4

1 回答 1

0

我用它来播放 MP3,很简单。

于 2010-08-13T03:53:09.833 回答