2016-04-22 8 views
0

Javaでoggオーディオファイルをループしようとしています。私はVorbisSPIを使用しています。oggファイルの最初の再生、2番目の再生でLineUnavailableExceptionが発生する

ファイルを1回再生できました。ファイルを再生したり、別のファイルを再生しようとすると、

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

Javaでmp3を再生するための[LineUnavailableException]の重複の可能性があります(http://stackoverflow.com/questions/3125934/lineunavailableexception-for-playing-mp3-with-java) – Wuaner

+0

@Wuaner、質問に記載されているとおり、一回、成功しました。 もう1つの質問では、OGGではなくMP3を扱います。一つの答えは私がすでに行っていることを示唆し、もう一つはMP3ファイルを再生するためにJLayerを使用しています。 – CarenRose

答えて

1

あなたは、あなたがそれで行われたときにlineclose()を呼び出す必要があります。

+0

ああ、私の良さは、すべてです。(家に帰ると、私は試してみるよ、ありがとう。 – CarenRose

関連する問題