// Copyright 2014-2025 Jesper Larsson
//
// This file is part of Klipspringer, <https://klipspringer.avadeaux.net/>
//
// Klipspringer is free software: you can redistribute it and/or modify it under the terms of the
// GNU General Public License as published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// Klipspringer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
// even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with Klipspringer. If
// not, see <https://www.gnu.org/licenses/>.

import java.io.File;
import javax.sound.sampled.*;

/** Simple audio file playing program, which is able to play a FLAC or Ogg Vorbis file if the
  * Klipspringer codec library is on the class/library path. Compile and run example:
  * <pre>
  *     javac examples/PlaySPI.java
  *     java -cp examples:lib/klipcodec.jar --enable-native-access=ALL-UNNAMED -Djava.library.path=lib PlaySPI some_audio_file.flac
  * </pre>
  */
class PlaySPI {
    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            System.err.println("Expecting file argument");
            System.exit(1);
        }

        AudioInputStream stream = AudioSystem.getAudioInputStream(new File(args[0]));
        SourceDataLine line = AudioSystem.getSourceDataLine(stream.getFormat());
        line.open();
        line.start();
        byte[] b = new byte[1024 * stream.getFormat().getFrameSize()];
        while (true) {
            int n = stream.read(b);
            if (n < 0) { break; }
            line.write(b, 0, n);
        }
        line.drain();
    }
}

Version: v4.3.2.2 (2026-05-16T17:03:34+02:00)
Raw file
Source code overview
Klipspringer home