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) {
}
}
}
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);
}
}
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();
}
}
}
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();
}
Aggregations