2016-04-22 46 views
0

我試圖在Java中循環ogg audiofiles。我正在使用VorbisSPI第一次玩ogg文件的作品,第二次玩LineUnavailableException

我成功地能夠播放文件一次。當我嘗試再次播放文件,或播放另一個文件,我得到一個

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); 
    } 
} 
+0

可能的重複[LineUnavailableException用於播放與Java的MP3](http://stackoverflow.com/questions/3125934/lineunavailableexception-for-playing-mp3-with-java) – Wuaner

+0

@Wuaner,如問題所述,它玩一次,成功。 在另一個問題中,它涉及MP3的,而不是OGG。一個答案建議做我已經做的事,另一個使用JLayer播放MP3文件。 – CarenRose

回答

1

你需要調用close()line當你用它做。

+0

哦,我的天啊,是這樣的:(我會在我回家時試試,謝謝。 – CarenRose

相關問題