-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSound.java
56 lines (48 loc) · 1.64 KB
/
Sound.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
package game.obj.sound;
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Sound {
private final URL shoot;
private final URL hit;
private final URL destroy;
public Sound() {
this.shoot = this.getClass().getClassLoader().getResource("game/obj/sound/shoot.wav");
this.hit = this.getClass().getClassLoader().getResource("game/obj/sound/hit.wav");
this.destroy = this.getClass().getClassLoader().getResource("game/obj/sound/destroy.wav");
}
public void soundShoot() {
play(shoot);
}
public void soundHit() {
play(hit);
}
public void soundDestroy() {
play(destroy);
}
private void play(URL url) {
try {
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.addLineListener(new LineListener() {
@Override
public void update(LineEvent event) {
if (event.getType() == LineEvent.Type.STOP) {
clip.close();
}
}
});
audioIn.close();
clip.start();
} catch (IOException | LineUnavailableException | UnsupportedAudioFileException e) {
System.err.println(e);
}
}
}