From ed66e0aef4f519052a982dfcd1d454a1699e9800 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Sun, 8 Feb 2009 14:15:36 -0500 Subject: Added Sound Effect support --- src/com/fourisland/fourpuzzle/Audio.java | 17 ++++++ .../fourisland/fourpuzzle/util/ObjectLoader.java | 63 ++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/src/com/fourisland/fourpuzzle/Audio.java b/src/com/fourisland/fourpuzzle/Audio.java index b364342..c8cd75b 100755 --- a/src/com/fourisland/fourpuzzle/Audio.java +++ b/src/com/fourisland/fourpuzzle/Audio.java @@ -6,12 +6,15 @@ package com.fourisland.fourpuzzle; import com.fourisland.fourpuzzle.util.ObjectLoader; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; import java.util.logging.Level; import java.util.logging.Logger; import javax.sound.midi.InvalidMidiDataException; import javax.sound.midi.MidiSystem; import javax.sound.midi.MidiUnavailableException; import javax.sound.midi.Sequencer; +import javax.sound.sampled.Clip; /** * @@ -85,5 +88,19 @@ public class Audio { } } + private static Executor soundExecutor = Executors.newCachedThreadPool(); + public static void playSound(String file) + { + final Clip temp = ObjectLoader.getSound(file); + temp.start(); + + soundExecutor.execute(new Runnable() { + public void run() { + temp.drain(); + temp.stop(); + } + }); + } + } diff --git a/src/com/fourisland/fourpuzzle/util/ObjectLoader.java b/src/com/fourisland/fourpuzzle/util/ObjectLoader.java index 58cf5ee..7f7128f 100755 --- a/src/com/fourisland/fourpuzzle/util/ObjectLoader.java +++ b/src/com/fourisland/fourpuzzle/util/ObjectLoader.java @@ -8,15 +8,24 @@ package com.fourisland.fourpuzzle.util; import com.fourisland.fourpuzzle.PuzzleApplication; import java.awt.Color; import java.awt.image.BufferedImage; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; +import java.util.Map.Entry; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.sound.midi.InvalidMidiDataException; import javax.sound.midi.MidiSystem; import javax.sound.midi.Sequence; +import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.Clip; +import javax.sound.sampled.DataLine; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; import org.jdesktop.application.ResourceMap; /** @@ -27,6 +36,21 @@ public class ObjectLoader { private static HashMap objectCache = new HashMap(); + static + { + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { + public void run() { + for (Entry o : objectCache.entrySet()) + { + if (o.getKey().startsWith("Sound/")) + { + ((Clip) o.getValue()).close(); + } + } + } + })); + } + public static BufferedImage getImage(String type, String name) { if (!objectCache.containsKey(type + "/" + name)) @@ -118,4 +142,43 @@ public class ObjectLoader { return (Sequence) objectCache.get("Music/" + name); } + public static Clip getSound(String name) + { + if (!objectCache.containsKey("Sound/" + name)) + { + ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap(); + String filename = rm.getResourcesDir() + "sound/" + name + ".wav"; + InputStream soundFile = rm.getClassLoader().getResourceAsStream(filename); + AudioInputStream ais = null; + try { + ais = AudioSystem.getAudioInputStream(soundFile); + } catch (UnsupportedAudioFileException ex) { + Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); + } catch (IOException ex) { + throw new ResourceNotFoundException("Sound", name); + } + + AudioFormat af = ais.getFormat(); + Clip line = null; + DataLine.Info info = new DataLine.Info(Clip.class, af); + try { + line = (Clip) AudioSystem.getLine(info); + } catch (LineUnavailableException ex) { + Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); + } + + try { + line.open(ais); + } catch (LineUnavailableException ex) { + Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); + } catch (IOException ex) { + Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); + } + + addToObjectCache("Sound", name, line); + } + + return (Clip) objectCache.get("Sound/" + name); + } + } -- cgit 1.4.1