use of javax.sound.sampled.LineUnavailableException in project JMRI by JMRI.
the class SoundUtil method playSoundBuffer.
/**
* Play a sound from a buffer
*
*/
public static void playSoundBuffer(byte[] wavData) {
// get characteristics from buffer
jmri.jmrit.sound.WavBuffer wb = new jmri.jmrit.sound.WavBuffer(wavData);
float sampleRate = wb.getSampleRate();
int sampleSizeInBits = wb.getSampleSizeInBits();
int channels = wb.getChannels();
boolean signed = wb.getSigned();
boolean bigEndian = wb.getBigEndian();
AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
SourceDataLine line;
// format is an AudioFormat object
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
// Handle the error.
log.warn("line not supported: " + info);
return;
}
// Obtain and open the line.
try {
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format);
} catch (LineUnavailableException ex) {
// Handle the error.
log.error("error opening line: " + ex);
return;
}
line.start();
// write(byte[] b, int off, int len)
line.write(wavData, 0, wavData.length);
}
Aggregations