-
Notifications
You must be signed in to change notification settings - Fork 0
/
Music.java
36 lines (31 loc) · 869 Bytes
/
Music.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
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
public class Music {
private Clip clip;
public Music(String filePath) {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(filePath));
clip = AudioSystem.getClip();
clip.open(audioInputStream);
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
e.printStackTrace();
}
}
public void play() {
if (clip != null) {
clip.setFramePosition(0);
clip.start();
}
}
public void stop() {
if (clip != null && clip.isRunning()) {
clip.stop();
}
}
public void loop() {
if (clip != null) {
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
}
}