-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSound.java
59 lines (52 loc) · 1.18 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
import java.io.File;
import javax.sound.sampled.*;
public class Sound {
private Clip clip;
public Sound(String fileName) {
// specify .wav file to play
// catch exceptions in cases of:
// -- file not found
// -- audio system disabled
try {
File file = new File(fileName);
if (file.exists()) {
AudioInputStream sound = AudioSystem.getAudioInputStream(file);
// load the sound into memory (a Clip)
clip = AudioSystem.getClip();
clip.open(sound);
}
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
// stop, reset timer, play clip
public void play() {
try {
clip.stop();
clip.setFramePosition(0); // rewind!
clip.start();
}
catch (Exception e) {
// If it doesn't work, just don't play the sound
}
}
// loop clip
public void loop() {
try {
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
catch (Exception e) {
//if it breaks, just don't play the sound
}
}
// stop clip
public void stop() {
try {
clip.stop();
}
catch (Exception e) {
// if it can't stop, then just play the entire sound
}
}
}