Example usage for javax.sound.sampled DataLine.Info DataLine.Info

List of usage examples for javax.sound.sampled DataLine.Info DataLine.Info

Introduction

In this page you can find the example usage for javax.sound.sampled DataLine.Info DataLine.Info.

Prototype

public Info(Class<?> lineClass, AudioFormat format) 

Source Link

Document

Constructs a data line's info object from the specified information, which includes a single audio format.

Usage

From source file:org.snitko.app.playback.PlaySound.java

public void play(File inputFile) {

    try (final AudioInputStream in = AudioSystem.getAudioInputStream(inputFile)) {

        final AudioFormat outFormat = getOutFormat(in.getFormat());
        final DataLine.Info info = new DataLine.Info(SourceDataLine.class, outFormat);

        try (final SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(info)) {

            if (sourceDataLine != null) {
                sourceDataLine.open(outFormat);
                sourceDataLine.start();//from   w w w.j  a v  a 2  s  .  co m
                AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(outFormat, in);
                stream(audioInputStream, sourceDataLine);
                sourceDataLine.drain();
                sourceDataLine.stop();
            }
        }

    } catch (UnsupportedAudioFileException | LineUnavailableException | IOException e) {
        throw new IllegalStateException(e);
    }
}