-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoundManager.java
84 lines (61 loc) · 2.12 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
84
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 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/LaserSound.wav"); // played when the laser cannon is fired
clips.put("fireLaser", clip);
clip = loadClip("sounds/LifeUp.wav"); // played when lives increase
clips.put("lifeUp", clip);
clip = loadClip("sounds/CityExplosion.wav"); // played when the missile hits the city
clips.put("cityExplosion", clip);
clip = loadClip("sounds/Panic.wav"); // played when the missile hits the city
clips.put("panic", clip);
clip = loadClip("sounds/MissileExplosion.wav"); // played when the laser cannon hits the missile
clips.put("missileExplosion", clip);
}
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();
}
}
}