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