-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioPlayer.java
More file actions
61 lines (46 loc) · 1.44 KB
/
AudioPlayer.java
File metadata and controls
61 lines (46 loc) · 1.44 KB
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
import javax.sound.sampled.*;
import java.io.*;
public class AudioPlayer {
private Clip clip;
public AudioPlayer(String filename) {
try {
// open the audio input stream
AudioInputStream stream = AudioSystem.getAudioInputStream(new File(filename));
AudioFormat format = stream.getFormat();
// specify what kind of line we want to create
DataLine.Info info = new DataLine.Info(Clip.class, format);
// create the line
clip = (Clip)AudioSystem.getLine(info);
//clip = AudioSystem.getClip();
// load the samples from the stream
clip.open(stream);
}
catch (UnsupportedAudioFileException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
catch (LineUnavailableException ex) {
ex.printStackTrace();
}
}
public void play() {
if (clip.isRunning())
stop();
clip.setFramePosition(0);
clip.start();
}
public void stop(){
if (clip.isRunning())
clip.stop();
clip.setFramePosition(0);
}
public void close(){
stop();
clip.close();
}
public void loop(){
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
}