Search in sources :

Example 1 with Info

use of javax.sound.sampled.Line.Info in project smarthome by eclipse.

the class AudioPlayer method run.

/**
 * This method plays the contained AudioSource
 */
@Override
public void run() {
    SourceDataLine line;
    AudioFormat audioFormat = convertAudioFormat(this.audioStream.getFormat());
    if (audioFormat == null) {
        logger.warn("Audio format is unsupported or does not have enough details in order to be played");
        return;
    }
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
    try {
        line = (SourceDataLine) AudioSystem.getLine(info);
        line.open(audioFormat);
    } catch (Exception e) {
        logger.warn("No line found: {}", e.getMessage());
        logger.info("Available lines are:");
        // get available mixers
        Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
        Mixer mixer = null;
        for (int cnt = 0; cnt < mixerInfo.length; cnt++) {
            mixer = AudioSystem.getMixer(mixerInfo[cnt]);
            Line.Info[] lineInfos = mixer.getSourceLineInfo();
            for (Info lineInfo : lineInfos) {
                logger.info("{}", lineInfo);
            }
        }
        return;
    }
    line.start();
    int nRead = 0;
    // needs to be a multiple of 4 and 6, to support both 16 and 24 bit stereo
    byte[] abData = new byte[65532];
    try {
        while (-1 != nRead) {
            nRead = audioStream.read(abData, 0, abData.length);
            if (nRead >= 0) {
                line.write(abData, 0, nRead);
            }
        }
    } catch (IOException e) {
        logger.error("Error while playing audio: {}", e.getMessage());
        return;
    } finally {
        line.drain();
        line.close();
        try {
            audioStream.close();
        } catch (IOException e) {
        }
    }
}
Also used : Line(javax.sound.sampled.Line) DataLine(javax.sound.sampled.DataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) DataLine(javax.sound.sampled.DataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) Mixer(javax.sound.sampled.Mixer) SourceDataLine(javax.sound.sampled.SourceDataLine) IOException(java.io.IOException) AudioFormat(javax.sound.sampled.AudioFormat) Info(javax.sound.sampled.Line.Info) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 AudioFormat (javax.sound.sampled.AudioFormat)1 DataLine (javax.sound.sampled.DataLine)1 Line (javax.sound.sampled.Line)1 Info (javax.sound.sampled.Line.Info)1 Mixer (javax.sound.sampled.Mixer)1 SourceDataLine (javax.sound.sampled.SourceDataLine)1