diff options
author | Starla Insigna <hatkirby@fourisland.com> | 2009-02-08 14:15:36 -0500 |
---|---|---|
committer | Starla Insigna <hatkirby@fourisland.com> | 2009-02-08 14:15:36 -0500 |
commit | ed66e0aef4f519052a982dfcd1d454a1699e9800 (patch) | |
tree | b1c77b520fdb90b0f7458f1e7aeec35446703462 /src/com/fourisland | |
parent | 7c8c6e2b37be39fbef4c168342fcdebc4ec58974 (diff) | |
download | fourpuzzle-ed66e0aef4f519052a982dfcd1d454a1699e9800.tar.gz fourpuzzle-ed66e0aef4f519052a982dfcd1d454a1699e9800.tar.bz2 fourpuzzle-ed66e0aef4f519052a982dfcd1d454a1699e9800.zip |
Added Sound Effect support
Diffstat (limited to 'src/com/fourisland')
-rwxr-xr-x | src/com/fourisland/fourpuzzle/Audio.java | 17 | ||||
-rwxr-xr-x | src/com/fourisland/fourpuzzle/util/ObjectLoader.java | 63 |
2 files changed, 80 insertions, 0 deletions
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 @@ | |||
6 | package com.fourisland.fourpuzzle; | 6 | package com.fourisland.fourpuzzle; |
7 | 7 | ||
8 | import com.fourisland.fourpuzzle.util.ObjectLoader; | 8 | import com.fourisland.fourpuzzle.util.ObjectLoader; |
9 | import java.util.concurrent.Executor; | ||
10 | import java.util.concurrent.Executors; | ||
9 | import java.util.logging.Level; | 11 | import java.util.logging.Level; |
10 | import java.util.logging.Logger; | 12 | import java.util.logging.Logger; |
11 | import javax.sound.midi.InvalidMidiDataException; | 13 | import javax.sound.midi.InvalidMidiDataException; |
12 | import javax.sound.midi.MidiSystem; | 14 | import javax.sound.midi.MidiSystem; |
13 | import javax.sound.midi.MidiUnavailableException; | 15 | import javax.sound.midi.MidiUnavailableException; |
14 | import javax.sound.midi.Sequencer; | 16 | import javax.sound.midi.Sequencer; |
17 | import javax.sound.sampled.Clip; | ||
15 | 18 | ||
16 | /** | 19 | /** |
17 | * | 20 | * |
@@ -85,5 +88,19 @@ public class Audio { | |||
85 | } | 88 | } |
86 | } | 89 | } |
87 | 90 | ||
91 | private static Executor soundExecutor = Executors.newCachedThreadPool(); | ||
92 | public static void playSound(String file) | ||
93 | { | ||
94 | final Clip temp = ObjectLoader.getSound(file); | ||
95 | temp.start(); | ||
96 | |||
97 | soundExecutor.execute(new Runnable() { | ||
98 | public void run() { | ||
99 | temp.drain(); | ||
100 | temp.stop(); | ||
101 | } | ||
102 | }); | ||
103 | } | ||
104 | |||
88 | } | 105 | } |
89 | 106 | ||
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; | |||
8 | import com.fourisland.fourpuzzle.PuzzleApplication; | 8 | import com.fourisland.fourpuzzle.PuzzleApplication; |
9 | import java.awt.Color; | 9 | import java.awt.Color; |
10 | import java.awt.image.BufferedImage; | 10 | import java.awt.image.BufferedImage; |
11 | import java.io.File; | ||
11 | import java.io.IOException; | 12 | import java.io.IOException; |
12 | import java.io.InputStream; | 13 | import java.io.InputStream; |
13 | import java.util.HashMap; | 14 | import java.util.HashMap; |
15 | import java.util.Map.Entry; | ||
14 | import java.util.logging.Level; | 16 | import java.util.logging.Level; |
15 | import java.util.logging.Logger; | 17 | import java.util.logging.Logger; |
16 | import javax.imageio.ImageIO; | 18 | import javax.imageio.ImageIO; |
17 | import javax.sound.midi.InvalidMidiDataException; | 19 | import javax.sound.midi.InvalidMidiDataException; |
18 | import javax.sound.midi.MidiSystem; | 20 | import javax.sound.midi.MidiSystem; |
19 | import javax.sound.midi.Sequence; | 21 | import javax.sound.midi.Sequence; |
22 | import javax.sound.sampled.AudioFormat; | ||
23 | import javax.sound.sampled.AudioInputStream; | ||
24 | import javax.sound.sampled.AudioSystem; | ||
25 | import javax.sound.sampled.Clip; | ||
26 | import javax.sound.sampled.DataLine; | ||
27 | import javax.sound.sampled.LineUnavailableException; | ||
28 | import javax.sound.sampled.UnsupportedAudioFileException; | ||
20 | import org.jdesktop.application.ResourceMap; | 29 | import org.jdesktop.application.ResourceMap; |
21 | 30 | ||
22 | /** | 31 | /** |
@@ -27,6 +36,21 @@ public class ObjectLoader { | |||
27 | 36 | ||
28 | private static HashMap<String,Object> objectCache = new HashMap<String,Object>(); | 37 | private static HashMap<String,Object> objectCache = new HashMap<String,Object>(); |
29 | 38 | ||
39 | static | ||
40 | { | ||
41 | Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { | ||
42 | public void run() { | ||
43 | for (Entry<String, Object> o : objectCache.entrySet()) | ||
44 | { | ||
45 | if (o.getKey().startsWith("Sound/")) | ||
46 | { | ||
47 | ((Clip) o.getValue()).close(); | ||
48 | } | ||
49 | } | ||
50 | } | ||
51 | })); | ||
52 | } | ||
53 | |||
30 | public static BufferedImage getImage(String type, String name) | 54 | public static BufferedImage getImage(String type, String name) |
31 | { | 55 | { |
32 | if (!objectCache.containsKey(type + "/" + name)) | 56 | if (!objectCache.containsKey(type + "/" + name)) |
@@ -118,4 +142,43 @@ public class ObjectLoader { | |||
118 | return (Sequence) objectCache.get("Music/" + name); | 142 | return (Sequence) objectCache.get("Music/" + name); |
119 | } | 143 | } |
120 | 144 | ||
145 | public static Clip getSound(String name) | ||
146 | { | ||
147 | if (!objectCache.containsKey("Sound/" + name)) | ||
148 | { | ||
149 | ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap(); | ||
150 | String filename = rm.getResourcesDir() + "sound/" + name + ".wav"; | ||
151 | InputStream soundFile = rm.getClassLoader().getResourceAsStream(filename); | ||
152 | AudioInputStream ais = null; | ||
153 | try { | ||
154 | ais = AudioSystem.getAudioInputStream(soundFile); | ||
155 | } catch (UnsupportedAudioFileException ex) { | ||
156 | Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); | ||
157 | } catch (IOException ex) { | ||
158 | throw new ResourceNotFoundException("Sound", name); | ||
159 | } | ||
160 | |||
161 | AudioFormat af = ais.getFormat(); | ||
162 | Clip line = null; | ||
163 | DataLine.Info info = new DataLine.Info(Clip.class, af); | ||
164 | try { | ||
165 | line = (Clip) AudioSystem.getLine(info); | ||
166 | } catch (LineUnavailableException ex) { | ||
167 | Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); | ||
168 | } | ||
169 | |||
170 | try { | ||
171 | line.open(ais); | ||
172 | } catch (LineUnavailableException ex) { | ||
173 | Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); | ||
174 | } catch (IOException ex) { | ||
175 | Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); | ||
176 | } | ||
177 | |||
178 | addToObjectCache("Sound", name, line); | ||
179 | } | ||
180 | |||
181 | return (Clip) objectCache.get("Sound/" + name); | ||
182 | } | ||
183 | |||
121 | } | 184 | } |