Search in sources :

Example 16 with LineUnavailableException

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);
}
Also used : DataLine(javax.sound.sampled.DataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) LineUnavailableException(javax.sound.sampled.LineUnavailableException) SourceDataLine(javax.sound.sampled.SourceDataLine) AudioFormat(javax.sound.sampled.AudioFormat)

Aggregations

LineUnavailableException (javax.sound.sampled.LineUnavailableException)16 DataLine (javax.sound.sampled.DataLine)11 SourceDataLine (javax.sound.sampled.SourceDataLine)7 IOException (java.io.IOException)6 AudioInputStream (javax.sound.sampled.AudioInputStream)6 AudioFormat (javax.sound.sampled.AudioFormat)5 Clip (javax.sound.sampled.Clip)3 Mixer (javax.sound.sampled.Mixer)3 TargetDataLine (javax.sound.sampled.TargetDataLine)3 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)3 FileNotFoundException (java.io.FileNotFoundException)2 URL (java.net.URL)2 Line (javax.sound.sampled.Line)2 Countdown (com.bixly.pastevid.screencap.components.capturebox.Countdown)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 FileInputStream (java.io.FileInputStream)1 MissingResourceException (java.util.MissingResourceException)1 MidiUnavailableException (javax.sound.midi.MidiUnavailableException)1 LineEvent (javax.sound.sampled.LineEvent)1