【问题标题】:First play of ogg file works, LineUnavailableException on second play第一次播放 ogg 文件有效,第二次播放 LineUnavailableException
【发布时间】:2016-08-15 11:47:10
【问题描述】:

我正在尝试在 Java 中循环播放 ogg 音频文件。我正在使用VorbisSPI

我成功地播放了一次文件。当我尝试再次播放该文件或播放另一个文件时,我得到一个

LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian not supported.

我不知道我做错了什么。

方法如下。

public static void testLine(File file) {
    try (AudioInputStream in = AudioSystem.getAudioInputStream(file)) {
        AudioFormat inFormat = in.getFormat();
        AudioFormat outFormat = new AudioFormat(PCM_SIGNED, inFormat.getSampleRate(), 
                16, inFormat.getChannels(), inFormat.getChannels() * 2, inFormat.getSampleRate(), false);
        Info info = new Info(SourceDataLine.class, outFormat);

        SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
        if (line != null) {
            line.open(outFormat);

            FloatControl volume = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN); 
            volume.setValue((float) ambiance.audio.Track.DEFAULT_VOLUME);

            // stream
            line.start();

            byte[] buffer = new byte[65536];        // is this magical?     // yes: the highest number which can be represented by an unsigned 16-bit binary number
            AudioInputStream stream = AudioSystem.getAudioInputStream(outFormat,in);
            for (int n = 0; n != -1; n = stream.read(buffer, 0, buffer.length)) {
                line.write(buffer, 0, n);
            }

            line.drain();
            line.stop();
            in.close();

            retVal = true;
        }
    } catch (UnsupportedAudioFileException|LineUnavailableException|IOException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), 
                e.getClass().toString(), JOptionPane.ERROR_MESSAGE);
    } 
}

【问题讨论】:

  • @Wuaner,如问题所述,它播放一次,成功。在另一个问题中,它处理的是 MP3,而不是 OGG。一个答案建议做我已经做的事情,另一个使用 JLayer 播放 MP3 文件。

标签: java javasound oggvorbis


【解决方案1】:

完成后,您需要在line 上致电close()

【讨论】:

  • 天哪,仅此而已:(我回家后试试,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-18
  • 1970-01-01
相关资源
最近更新 更多