-
Notifications
You must be signed in to change notification settings - Fork 0
/
Playmusic.java
78 lines (61 loc) · 1.89 KB
/
Playmusic.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
/*
*
* Class for playing WAV files
*
*/
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Playmusic implements Runnable {
//main method only used for sound testing
public static void main(String[] args){
Thread t = new Thread(new Playmusic("redalert.wav", 1));
t.start();
}
//path of the file to play
private String path;
//denotes weather the file will loop or not
private int loops;
//constructor
public Playmusic(String s, int i) {
//sets file based on path of installed sounds
path = "/opt/sounds/" + s;
//loops continuously if passed loop value is not 0
loops = (i == 0) ? 0 : Clip.LOOP_CONTINUOUSLY;
}
//runs the thread
@Override
public void run() {
//file and stream information
AudioInputStream audioIn = null;
Clip clip = null;
try {
//assign file, stream and looping information
audioIn = AudioSystem.getAudioInputStream(new File(path));
clip = AudioSystem.getClip();
clip.open(audioIn);
clip.loop(loops);
//start playback
clip.start();
//keeps the thread alive while the sound is playing
do Thread.sleep(clip.getMicrosecondLength()/1000);
//keeps thread alive for consecutive loops
while (loops < 0);
//close file and stream
clip.close();
audioIn.close();
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException
| InterruptedException | IllegalArgumentException e1) {
}
//close any file / stream if open after error handling
try {
if (clip != null && clip.isOpen()) clip.close();
if (!audioIn.equals(null)) audioIn.close();
} catch (IOException e) {
}
}
}