-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundManager.java
More file actions
137 lines (125 loc) · 2.55 KB
/
SoundManager.java
File metadata and controls
137 lines (125 loc) · 2.55 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
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
/**
* This is my class I use to play my sound in my game,
* all music and sound effects are handled here
* @author elan
*
*/
public class SoundManager implements Runnable
{
/**
* Some private class variables that store the audio that is going to be played
*/
private static boolean isRunning = false;
private static boolean first = true;
private static AudioInputStream audioIn;
private static Clip currentSong;
private static Clip currentSound;
/**
* Constructor for the SoundManager Class
*/
public SoundManager()
{
isRunning = true;
new Thread(this).start();
}
/**
* One of the most important methods in SoundManager,
* it takes the file name of the sound file you want played and plays it,
* this should stricktly be used for short sound effects
* @param filename
*/
private void play(String filename)
{
try
{
audioIn = AudioSystem.getAudioInputStream(new File(filename));
currentSound = AudioSystem.getClip();
currentSound.open(audioIn);
currentSound.start();
audioIn = null;
currentSound = null;
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* One of the most important methods in SoundManager,
* it takes the file name of the sound file you want played and plays it,
* Should be used for music
* @param filename
* @param songLength in mili-seconds
*/
private void loop(String filename, long songLength)
{
try
{
audioIn = AudioSystem.getAudioInputStream(getClass().getResource(filename));
currentSong = AudioSystem.getClip();
currentSong.open(audioIn);
currentSong.start();
Thread.sleep(songLength);
audioIn = null;
currentSong = null;
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* playes one of the two songs that are pre loaded in
*/
public void playGameMusic()
{
if((int)(Math.random()*2)%2 == 0)
loop("inGame.wav", 506000);
else
loop("inGame2.wav", 279000);
}
public void playMenuMusic()
{
loop("menues.wav", 133000);
}
public void playShoot()
{
play("shoot.wav");
}
public void playClick()
{
play("click.wav");
}
public void playPowerUp()
{
play("power-up.wav");
}
public void playExplode()
{
play("explode.wav");
}
public void run()
{
while(isRunning)
{
if(first)
{
playMenuMusic();
first = false;
}
else
playGameMusic();
try
{
Thread.sleep(1000/(States.FPS*2));
}
catch(Exception e)
{
}
}
}
}