Example usage for javax.sound.sampled LineEvent getLine

List of usage examples for javax.sound.sampled LineEvent getLine

Introduction

In this page you can find the example usage for javax.sound.sampled LineEvent getLine.

Prototype

public final Line getLine() 

Source Link

Document

Obtains the audio line that is the source of this event.

Usage

From source file:ClipTest.java

public static void main(String[] args) throws Exception {

    // specify the sound to play
    // (assuming the sound can be played by the audio system)
    File soundFile = new File("../sounds/voice.wav");
    AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);

    // load the sound into memory (a Clip)
    DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
    Clip clip = (Clip) AudioSystem.getLine(info);
    clip.open(sound);/*from www  .  j  a v a2s.co m*/

    // due to bug in Java Sound, explicitly exit the VM when
    // the sound has stopped.
    clip.addLineListener(new LineListener() {
        public void update(LineEvent event) {
            if (event.getType() == LineEvent.Type.STOP) {
                event.getLine().close();
                System.exit(0);
            }
        }
    });

    // play the sound clip
    clip.start();
}

From source file:org.yccheok.jstock.gui.Utils.java

public static void playAlertSound() {
    new Thread(new Runnable() {
        @Override//from  ww  w.j  a  va 2 s. c om
        public void run() {
            try {
                Clip clip = AudioSystem.getClip();
                clip.addLineListener(new LineListener() {
                    @Override
                    public void update(LineEvent event) {
                        if (event.getType() == LineEvent.Type.STOP) {
                            event.getLine().close();
                        }
                    }
                });
                final InputStream audioSrc = Utils.class.getResourceAsStream("/assets/sounds/doorbell.wav");
                // http://stackoverflow.com/questions/5529754/java-io-ioexception-mark-reset-not-supported
                // Add buffer for mark/reset support.
                final InputStream bufferedIn = new BufferedInputStream(audioSrc);
                AudioInputStream inputStream = AudioSystem.getAudioInputStream(bufferedIn);
                clip.open(inputStream);
                clip.start();
            } catch (Exception e) {
                log.error(null, e);
            }
        }
    }).start();
}