0

I'm attempting to loop ogg audiofiles in Java. I'm using the VorbisSPI.

I was successfully able to play a file ONCE. When I try to play the file again, or play another file, I get a

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

I don't know what I'm doing wrong.

Method is below.

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);
    } 
}
CarenRose
  • 1,266
  • 1
  • 12
  • 24
  • Possible duplicate of [LineUnavailableException for playing mp3 with java](http://stackoverflow.com/questions/3125934/lineunavailableexception-for-playing-mp3-with-java) – Wuaner Apr 22 '16 at 02:34
  • @Wuaner, as stated in the question, it plays once, successfully. In the other question, it deals with MP3's, not OGG. One answer suggests doing what I already do, and the other uses JLayer to play MP3 files. – CarenRose Apr 22 '16 at 03:42

1 Answers1

2

You need to call close() on the line when you are done with it.

greg-449
  • 109,219
  • 232
  • 102
  • 145