Search in sources :

Example 31 with SourceDataLine

use of javax.sound.sampled.SourceDataLine 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)

Example 32 with SourceDataLine

use of javax.sound.sampled.SourceDataLine in project lavaplayer by sedmelluq.

the class LocalPlayerDemo method main.

public static void main(String[] args) throws LineUnavailableException, IOException {
    AudioPlayerManager manager = new DefaultAudioPlayerManager();
    AudioSourceManagers.registerRemoteSources(manager);
    manager.getConfiguration().setOutputFormat(COMMON_PCM_S16_BE);
    AudioPlayer player = manager.createPlayer();
    manager.loadItem("ytsearch: epic soundtracks", new FunctionalResultHandler(null, playlist -> {
        player.playTrack(playlist.getTracks().get(0));
    }, null, null));
    AudioDataFormat format = manager.getConfiguration().getOutputFormat();
    AudioInputStream stream = AudioPlayerInputStream.createStream(player, format, 10000L, false);
    SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class, stream.getFormat());
    SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
    line.open(stream.getFormat());
    line.start();
    byte[] buffer = new byte[COMMON_PCM_S16_BE.maximumChunkSize()];
    int chunkSize;
    while ((chunkSize = stream.read(buffer)) >= 0) {
        line.write(buffer, 0, chunkSize);
    }
}
Also used : AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) DefaultAudioPlayerManager(com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager) LineUnavailableException(javax.sound.sampled.LineUnavailableException) IOException(java.io.IOException) DataLine(javax.sound.sampled.DataLine) AudioPlayerManager(com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager) AudioPlayerInputStream(com.sedmelluq.discord.lavaplayer.format.AudioPlayerInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) AudioSourceManagers(com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers) AudioDataFormat(com.sedmelluq.discord.lavaplayer.format.AudioDataFormat) COMMON_PCM_S16_BE(com.sedmelluq.discord.lavaplayer.format.StandardAudioDataFormats.COMMON_PCM_S16_BE) AudioSystem(javax.sound.sampled.AudioSystem) FunctionalResultHandler(com.sedmelluq.discord.lavaplayer.player.FunctionalResultHandler) SourceDataLine(javax.sound.sampled.SourceDataLine) AudioInputStream(javax.sound.sampled.AudioInputStream) AudioDataFormat(com.sedmelluq.discord.lavaplayer.format.AudioDataFormat) AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) SourceDataLine(javax.sound.sampled.SourceDataLine) DefaultAudioPlayerManager(com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager) FunctionalResultHandler(com.sedmelluq.discord.lavaplayer.player.FunctionalResultHandler) DefaultAudioPlayerManager(com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager) AudioPlayerManager(com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager)

Example 33 with SourceDataLine

use of javax.sound.sampled.SourceDataLine in project algs4 by kevin-wayne.

the class StdAudio method stream.

// https://www3.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html
// play a wav or aif file
// javax.sound.sampled.Clip fails for long clips (on some systems), perhaps because
// JVM closes (see remedy in loop)
private static void stream(AudioInputStream ais) {
    SourceDataLine line = null;
    // 4K buffer
    int BUFFER_SIZE = 4096;
    try {
        AudioFormat audioFormat = ais.getFormat();
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
        line = (SourceDataLine) AudioSystem.getLine(info);
        line.open(audioFormat);
        line.start();
        byte[] samples = new byte[BUFFER_SIZE];
        int count = 0;
        while ((count = ais.read(samples, 0, BUFFER_SIZE)) != -1) {
            line.write(samples, 0, count);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (LineUnavailableException e) {
        e.printStackTrace();
    } finally {
        if (line != null) {
            line.drain();
            line.close();
        }
    }
}
Also used : DataLine(javax.sound.sampled.DataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) LineUnavailableException(javax.sound.sampled.LineUnavailableException) SourceDataLine(javax.sound.sampled.SourceDataLine) IOException(java.io.IOException) AudioFormat(javax.sound.sampled.AudioFormat)

Example 34 with SourceDataLine

use of javax.sound.sampled.SourceDataLine in project algs4 by kevin-wayne.

the class StdAudio method init.

// open up an audio stream
private static void init() {
    try {
        // 44,100 Hz, 16-bit audio, mono, signed PCM, little endian
        AudioFormat format = new AudioFormat((float) SAMPLE_RATE, BITS_PER_SAMPLE, MONO, SIGNED, LITTLE_ENDIAN);
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
        line = (SourceDataLine) AudioSystem.getLine(info);
        line.open(format, SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE);
        // the internal buffer is a fraction of the actual buffer size, this choice is arbitrary
        // it gets divided because we can't expect the buffered data to line up exactly with when
        // the sound card decides to push out its samples.
        buffer = new byte[SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE / 3];
    } catch (LineUnavailableException e) {
        System.out.println(e.getMessage());
    }
    // no sound gets made before this call
    line.start();
}
Also used : DataLine(javax.sound.sampled.DataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) LineUnavailableException(javax.sound.sampled.LineUnavailableException) AudioFormat(javax.sound.sampled.AudioFormat)

Aggregations

SourceDataLine (javax.sound.sampled.SourceDataLine)34 LineUnavailableException (javax.sound.sampled.LineUnavailableException)21 AudioFormat (javax.sound.sampled.AudioFormat)19 DataLine (javax.sound.sampled.DataLine)19 IOException (java.io.IOException)14 AudioInputStream (javax.sound.sampled.AudioInputStream)14 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)5 Line (javax.sound.sampled.Line)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Mixer (javax.sound.sampled.Mixer)3 MpegAudioFormat (javazoom.spi.mpeg.sampled.file.MpegAudioFormat)3 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 TargetDataLine (javax.sound.sampled.TargetDataLine)2 AudioDataFormat (com.sedmelluq.discord.lavaplayer.format.AudioDataFormat)1 AudioPlayerInputStream (com.sedmelluq.discord.lavaplayer.format.AudioPlayerInputStream)1 COMMON_PCM_S16_BE (com.sedmelluq.discord.lavaplayer.format.StandardAudioDataFormats.COMMON_PCM_S16_BE)1 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)1 AudioPlayerManager (com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager)1 DefaultAudioPlayerManager (com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager)1