Example usage for javax.sound.sampled AudioSystem getClip

List of usage examples for javax.sound.sampled AudioSystem getClip

Introduction

In this page you can find the example usage for javax.sound.sampled AudioSystem getClip.

Prototype

public static Clip getClip() throws LineUnavailableException 

Source Link

Document

Obtains a clip that can be used for playing back an audio file or an audio stream.

Usage

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

public static void playAlertSound() {
    new Thread(new Runnable() {
        @Override/*from   www .  j av a  2  s .  c o  m*/
        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();
}