-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSound.java
72 lines (67 loc) · 1.46 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
package beta;
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
public class Sound {
private Clip clip;
//Get the sound file and creates clip.
public Sound (String fileName) {
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(new File(fileName));
clip = AudioSystem.getClip();
clip.open(ais);
FloatControl gainControl =
(FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(-15.0f);
} catch (Exception e) {
e.printStackTrace();
}
}
//stop clip
public void stop(){
if(clip == null) return;
clip.stop();
}
//method to play audio file once
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();
}
}
//method to play audio file continuously
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();
}
}
//check if clip is still playing
public boolean isActive(){
return clip.isActive();
}
}