-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundManager.java
More file actions
162 lines (118 loc) · 3.72 KB
/
Copy pathSoundManager.java
File metadata and controls
162 lines (118 loc) · 3.72 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import javax.sound.sampled.AudioInputStream; // for playing sound clips
import javax.sound.sampled.*;
import java.io.*;
import java.util.HashMap; // for storing sound clips
public class SoundManager { // a Singleton class
HashMap<String, Clip> clips;
private static SoundManager instance = null; // keeps track of Singleton instance
private boolean isActive = false;
private float volume;
private SoundManager () {
clips = new HashMap<String, Clip>();
Clip clip = loadClip("sounds/bgMusic.wav");
clips.put("background", clip);
clip = loadClip("sounds/collectKeys.wav");
clips.put("collect", clip);
clip = loadClip("sounds/dieSound.wav");
clips.put("dead", clip);
clip = loadClip("sounds/jump.wav");
clips.put("jumping", clip);
clip = loadClip("sounds/runningInGrass.wav");
clips.put("running", clip);
clip = loadClip("sounds/swordSlashEnemy.wav");
clips.put("enemySlash", clip);
clip = loadClip("sounds/swordSlashPlayer.wav");
clips.put("playerSlash", clip);
clip = loadClip("sounds/warpSound.wav");
clips.put("warp", clip);
clip = loadClip("sounds/winSound.wav");
clips.put("win", clip);
clip = loadClip("sounds/playerHit.wav");
clips.put("playerHurt", clip);
clip = loadClip("sounds/enemyHit.wav");
clips.put("enemyHurt", clip);
clip = loadClip("sounds/killed.wav");
clips.put("enemyDead", clip);
clip = loadClip("sounds/runningInGrass.wav");
clips.put("runningEnemy", clip);
clip = loadClip("sounds/aggro.wav");
clips.put("detected", clip);
volume = 0.3f;
}
public static SoundManager getInstance() { // class method to retrieve instance of Singleton
if (instance == null)
instance = new SoundManager();
return instance;
}
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 Clip getClip (String title) {
return clips.get(title);
}
public void playClip(String title, boolean looping) {
Clip clip = getClip(title);
if (clip != null) {
clip.setFramePosition(0);
FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
float dB = (float) (Math.log(volume) / Math.log(10.0) * 20.0);
gainControl.setValue(dB);
if (looping)
clip.loop(Clip.LOOP_CONTINUOUSLY);
else
clip.start();
isActive= true;
}
}
public void stopClip(String title) {
Clip clip = getClip(title);
if (clip != null) {
clip.stop();
isActive=false;
}
}
public boolean checkClipStatus()
{
return isActive;
}
public void pauseClip(String title) {
Clip clip = getClip(title);
if (clip != null && clip.isRunning()) {
clip.stop();
}
}
public void resumeClip(String title, boolean looping) {
Clip clip = getClip(title);
if (clip != null && !clip.isRunning()) {
clip.start();
FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
float dB = (float) (Math.log(volume) / Math.log(10.0) * 20.0);
gainControl.setValue(dB);
if(looping==true)
{
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
}
}
public void playOrResumeClip(String title, boolean looping) {
Clip clip = getClip(title);
if (clip != null) {
if (clip.isRunning()) {
resumeClip(title, looping);
} else {
playClip(title, looping);
}
}
}
}