tags:

views:

43

answers:

1

From http://stackoverflow.com/questions/26305/how-can-i-play-sound-in-java

Is it possible to have resouce leakage for the following code? Who will be responsible to close the audio stream?

public static void playAlertSound() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Clip clip = AudioSystem.getClip();
                AudioInputStream inputStream = AudioSystem.getAudioInputStream(Utils.class.getResourceAsStream("/sounds/door_bell.wav"));
                clip.open(inputStream);
                clip.start();
            } catch (Exception e) {
               log.error(null, e);
            }
        }
    }).start();
}
+2  A: 

You need to explicitly close the line as follows:

clip.addLineListener(new LineListener() {
    public void update(LineEvent event) {
        if (event.getType() == LineEvent.Type.STOP) {
            event.getLine().close();
        }
    }
});
Pool
How about AudioInputStream? Do I need to handle it as well?
Yan Cheng CHEOK