-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoundManager.java
83 lines (64 loc) · 1.81 KB
/
SoundManager.java
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.*;
import java.io.*;
import java.util.HashMap;
public class SoundManager {
HashMap<String, Clip> clips;
Clip hitClip = null;
Clip appearClip = null;
Clip backgroundClip = null;
private static SoundManager instance = null;
private SoundManager () {
clips = new HashMap<String, Clip>();
Clip clip = loadClip("./sounds/kick.wav");
clips.put("kick", clip);
clip = loadClip("./sounds/whistle.wav");
clips.put("whistle", clip);
clip = loadClip("./sounds/game_intro.wav");
clips.put("game_intro", clip);
clip = loadClip("./sounds/goal_chant.wav");
clips.put("goal", clip);
clip = loadClip("./sounds/goal_missed.wav");
clips.put("missed", clip);
clip = loadClip("./sounds/backg.wav");
clips.put("background", clip);
}
public static SoundManager getInstance() {
if (instance == null)
instance = new SoundManager();
return instance;
}
public Clip getClip (String title) {
return clips.get(title);
}
public Clip loadClip (String fileName) { // gets clip from the specified file
AudioInputStream audioIn;
Clip clip = null;
try {
File file = new File(fileName);
audioIn = AudioSystem.getAudioInputStream(file.toURI().toURL());
clip = AudioSystem.getClip();
clip.open(audioIn);
}
catch (Exception e) {
System.out.println ("Error opening sound files: " + e);
}
return clip;
}
public void playSound(String title, Boolean looping) {
Clip clip = getClip(title);
if (clip != null) {
clip.setFramePosition(0);
if (looping)
clip.loop(Clip.LOOP_CONTINUOUSLY);
else
clip.start();
}
}
public void stopSound(String title) {
Clip clip = getClip(title);
if (clip != null) {
clip.stop();
}
}
}