-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundManager.java
More file actions
127 lines (92 loc) · 3.03 KB
/
SoundManager.java
File metadata and controls
127 lines (92 loc) · 3.03 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
// for playing sound clips
import javax.sound.sampled.*;
import java.io.*;
import java.util.HashMap; // for storing sound clips
import java.util.Objects;
public class SoundManager { // a Singleton class
HashMap<String, Clip> clips;
private static SoundManager instance = null; // keeps track of Singleton instance
private float volume;
private SoundManager () {
clips = new HashMap<String, Clip>();
Clip clip = loadClip("sounds/background.wav"); // played from start of the game
clips.put("background", clip);
clip = loadClip("sounds/enemy_hit.wav");
clips.put("hit", clip);
clip = loadClip("sounds/player_hit.wav");
clips.put("playerHit", clip);
clip = loadClip("sounds/shoot.wav");
clips.put("shoot", clip);
clip = loadClip("sounds/death.wav");
clips.put("death", clip);
clip = loadClip("sounds/kamikaze.wav");
clips.put("kamikaze", clip);
clip = loadClip("sounds/powerup.wav");
clips.put("powerup", clip);
clip = loadClip("sounds/pickup.wav");
clips.put("pickup", clip);
clip = loadClip("sounds/takeoff.wav");
clips.put("takeoff", clip);
clip = loadClip("sounds/engine.wav");
clips.put("engine", clip);
clip = loadClip("sounds/wind.wav");
clips.put("wind", clip);
clip = loadClip("sounds/background2.wav");
clips.put("country2", clip);
clip = loadClip("sounds/background3.wav");
clips.put("country3", clip);
clip = loadClip("sounds/backgroundfull.wav");
clips.put("backgroundfull", clip);
volume = 1.0f;
}
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);
if (looping)
clip.loop(Clip.LOOP_CONTINUOUSLY);
else
clip.start();
}
}
public void stopClip(String title) {
Clip clip = getClip(title);
if (clip != null) {
clip.stop();
}
}
public void setVolume (String title, float volume) {
Clip clip = getClip(title);
if (clip == null) return;
if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
float range = gainControl.getMaximum() - gainControl.getMinimum();
float gain = (range * volume) + gainControl.getMinimum();
gainControl.setValue(gain);
} else {
System.out.println("Volume control not supported for: " + title);
}
}
}