-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sound
78 lines (65 loc) · 1.39 KB
/
Sound
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
package framework;
import java.io.BufferedInputStream;
import java.io.InputStream;
import javax.sound.sampled.*;
/**
* Gets the sound file to play background sound
* @author ilaydabuyukdogan
*
*/
public class Sound {
private Clip clip;
// Change file name to match yours, of course
public static Sound sound1 = new Sound("/Users/ilaydabuyukdogan/eclipse-workspace/marioP1/src/Music/Background.wav");
public Sound (String fileName) {
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(Sound.class.getResource(fileName));
clip = AudioSystem.getClip();
clip.open(ais);
} catch (Exception e) {
e.printStackTrace();
}
}
public void play() {
try {
if (clip != null) {
new Thread() {
public void run() {
synchronized (clip) {
clip.stop();
clip.setFramePosition(0);
clip.start();
}
}
}.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void stop(){
if(clip == null) return;
clip.stop();
}
public void loop() {
try {
if (clip != null) {
new Thread() {
public void run() {
synchronized (clip) {
clip.stop();
clip.setFramePosition(0);
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
}
}.start();
}
} catch (Exception e) {
e.printStackTrace();
}
Sound.sound1.play();
}
public boolean isActive(){
return clip.isActive();
}
}