-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSoundPlayer.java
More file actions
69 lines (55 loc) · 1.62 KB
/
Copy pathSoundPlayer.java
File metadata and controls
69 lines (55 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
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;
public class SoundPlayer {
Queue<SOUNDS> soundQueue = new LinkedList<>();
private Map<SOUNDS, Clip> soundMap = new HashMap<>();
public SoundPlayer(String config_file) throws IOException, UnsupportedAudioFileException, LineUnavailableException {
BufferedReader br = new BufferedReader(new FileReader(config_file));
while(br.ready()){
String[] data = br.readLine().split("\\|");
Clip c = AudioSystem.getClip();
soundMap.put(SOUNDS.valueOf(data[0]), c);
AudioInputStream audioStream = AudioSystem.getAudioInputStream(new File(data[1]));
c.open(audioStream);
}
}
public static enum SOUNDS {
BONK, ITEM_PICKUP, SQUEEK, CLAP, DEATH
}
public void play(SOUNDS... sounds) {
for(SOUNDS s: sounds) {
soundQueue.offer(s);
}
play();
}
public void play() {
SOUNDS s = null;
while((s = soundQueue.poll())!=null) {
Clip c = soundMap.get(s);
//Restart the clip
if(c.isRunning()) {
c.stop();
}
//Queue it up
c.setFramePosition(0);
//Hit it!
c.start();
while(c.isRunning()) {
try {
Thread.sleep(100);
} catch (Exception e) {}
}
}
}
}