-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSound.java
96 lines (67 loc) · 1.62 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
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
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
/**
*
* Thanks to Dj CUTMAN for his awesome background music
*
*/
public class Sound {
public static Sound music = loadSound("/sounds/music.wav");
public static Sound invaderkilled = loadSound("/sounds/invaderkilled.wav");
public static Sound shoot = loadSound("/sounds/shoot.wav");
public static Sound explosion = loadSound("/sounds/explosion.wav");
public static Sound currentMusic;
private Clip clip;
public static Sound loadSound(String fileName) {
Sound sound = new Sound();
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(Sound.class.getResource(fileName));
Clip clip = AudioSystem.getClip();
clip.open(ais);
sound.clip = clip;
} catch (Exception e) {
System.out.println(e);
}
return sound;
}
public void play() {
try {
if (clip != null) {
clip.stop();
clip.setFramePosition(0);
clip.start();
}
} catch (Exception e) {
System.out.println(e);
}
}
public void stop(){
clip.stop();
}
private boolean isFinish(){
if (clip.getFrameLength() - clip.getFramePosition() < 1){
return true;
}
else {
return false;
}
}
public static void setCurrentMusic(Sound s){
if (Sound.currentMusic != null){
if (Sound.currentMusic == s){
if (Sound.currentMusic.isFinish()){
Sound.currentMusic.play();
}
}
else {
Sound.currentMusic.stop();
Sound.currentMusic = null;
}
}
if (Sound.currentMusic==null){
Sound.currentMusic = s;
Sound.currentMusic.play();
}
}
}