summary refs log tree commit diff stats
path: root/src/com
diff options
context:
space:
mode:
Diffstat (limited to 'src/com')
-rwxr-xr-xsrc/com/fourisland/fourpuzzle/Audio.java17
-rwxr-xr-xsrc/com/fourisland/fourpuzzle/util/ObjectLoader.java63
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 @@
6package com.fourisland.fourpuzzle; 6package com.fourisland.fourpuzzle;
7 7
8import com.fourisland.fourpuzzle.util.ObjectLoader; 8import com.fourisland.fourpuzzle.util.ObjectLoader;
9import java.util.concurrent.Executor;
10import java.util.concurrent.Executors;
9import java.util.logging.Level; 11import java.util.logging.Level;
10import java.util.logging.Logger; 12import java.util.logging.Logger;
11import javax.sound.midi.InvalidMidiDataException; 13import javax.sound.midi.InvalidMidiDataException;
12import javax.sound.midi.MidiSystem; 14import javax.sound.midi.MidiSystem;
13import javax.sound.midi.MidiUnavailableException; 15import javax.sound.midi.MidiUnavailableException;
14import javax.sound.midi.Sequencer; 16import javax.sound.midi.Sequencer;
17import 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;
8import com.fourisland.fourpuzzle.PuzzleApplication; 8import com.fourisland.fourpuzzle.PuzzleApplication;
9import java.awt.Color; 9import java.awt.Color;
10import java.awt.image.BufferedImage; 10import java.awt.image.BufferedImage;
11import java.io.File;
11import java.io.IOException; 12import java.io.IOException;
12import java.io.InputStream; 13import java.io.InputStream;
13import java.util.HashMap; 14import java.util.HashMap;
15import java.util.Map.Entry;
14import java.util.logging.Level; 16import java.util.logging.Level;
15import java.util.logging.Logger; 17import java.util.logging.Logger;
16import javax.imageio.ImageIO; 18import javax.imageio.ImageIO;
17import javax.sound.midi.InvalidMidiDataException; 19import javax.sound.midi.InvalidMidiDataException;
18import javax.sound.midi.MidiSystem; 20import javax.sound.midi.MidiSystem;
19import javax.sound.midi.Sequence; 21import javax.sound.midi.Sequence;
22import javax.sound.sampled.AudioFormat;
23import javax.sound.sampled.AudioInputStream;
24import javax.sound.sampled.AudioSystem;
25import javax.sound.sampled.Clip;
26import javax.sound.sampled.DataLine;
27import javax.sound.sampled.LineUnavailableException;
28import javax.sound.sampled.UnsupportedAudioFileException;
20import org.jdesktop.application.ResourceMap; 29import 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}