-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSoundEffect.java
More file actions
69 lines (45 loc) · 978 Bytes
/
SoundEffect.java
File metadata and controls
69 lines (45 loc) · 978 Bytes
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
import javax.sound.sampled.*;
public class SoundEffect {
private Clip clip;
public SoundEffect(String s) {
try {
AudioInputStream ais =
AudioSystem.getAudioInputStream(
getClass().getResourceAsStream(
s
)
);
AudioFormat baseFormat = ais.getFormat();
AudioFormat decodeFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
false
);
AudioInputStream dais =
AudioSystem.getAudioInputStream(
decodeFormat, ais);
clip = AudioSystem.getClip();
clip.open(dais);
}
catch(Exception e) {
e.printStackTrace();
}
}
public void play() {
if(clip == null) return;
stop();
clip.setFramePosition(0);
clip.start();
}
public void stop() {
if(clip.isRunning()) clip.stop();
}
public void close() {
stop();
clip.close();
}
}