From 1727579f84c9647c2cfe5844d37306f11c990eb9 Mon Sep 17 00:00:00 2001 From: Victor Date: Tue, 25 Mar 2014 11:27:34 +0200 Subject: [PATCH] Fix sound playing --- src/com/annimon/turrets/Sound.java | 117 +++++++++++++++-------------- 1 file changed, 60 insertions(+), 57 deletions(-) diff --git a/src/com/annimon/turrets/Sound.java b/src/com/annimon/turrets/Sound.java index 1a0a8d8..d2b3d3b 100644 --- a/src/com/annimon/turrets/Sound.java +++ b/src/com/annimon/turrets/Sound.java @@ -1,57 +1,60 @@ -package com.annimon.turrets; - -import com.annimon.turrets.util.ExceptionHandler; -import java.io.IOException; -import java.net.URL; -import javax.sound.sampled.AudioInputStream; -import javax.sound.sampled.AudioSystem; -import javax.sound.sampled.Clip; -import javax.sound.sampled.LineUnavailableException; -import javax.sound.sampled.UnsupportedAudioFileException; - -/** - * - * @author aNNiMON - */ -public enum Sound { - CLICK("/res/click.wav"), - EXPLOSION_1("/res/explosion1.wav"), - EXPLOSION_2("/res/explosion2.wav"); - - private static boolean enabled; - private final Clip soundClip; - - Sound(String resource) { - soundClip = loadClip(resource); - } - - public static boolean isEnabled() { - return enabled; - } - - public static void setEnabled(boolean enabled) { - Sound.enabled = enabled; - } - - public void play() { - if (enabled) { - soundClip.setFramePosition(0); - soundClip.start(); - } - } - - private Clip loadClip(String resource) { - Clip clip = null; - try { - final URL url = getClass().getResource(resource); - try (AudioInputStream ais = AudioSystem.getAudioInputStream(url)) { - clip = AudioSystem.getClip(); - clip.open(ais); - } - } catch (IOException | UnsupportedAudioFileException | LineUnavailableException ex) { - ExceptionHandler.handle(ex); - } - return clip; - } - -} +package com.annimon.turrets; + +import com.annimon.turrets.util.ExceptionHandler; +import java.io.IOException; +import java.net.URL; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.Clip; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; + +/** + * + * @author aNNiMON + */ +public enum Sound { + CLICK("/res/click.wav"), + EXPLOSION_1("/res/explosion1.wav"), + EXPLOSION_2("/res/explosion2.wav"); + + private static boolean enabled; + private final Clip soundClip; + + Sound(String resource) { + soundClip = loadClip(resource); + } + + public static boolean isEnabled() { + return enabled; + } + + public static void setEnabled(boolean enabled) { + Sound.enabled = enabled; + } + + public void play() { + if (enabled && (soundClip != null)) { + soundClip.setFramePosition(0); + soundClip.start(); + } + } + + private Clip loadClip(String resource) { + Clip clip = null; + try { + final URL url = getClass().getResource(resource); + try (AudioInputStream ais = AudioSystem.getAudioInputStream(url)) { + clip = AudioSystem.getClip(); + clip.open(ais); + } + } catch (IllegalArgumentException iae) { + Sound.setEnabled(false); + ExceptionHandler.handle(iae); + } catch (IOException | UnsupportedAudioFileException | LineUnavailableException ex) { + ExceptionHandler.handle(ex); + } + return clip; + } + +}