-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundManager.java
More file actions
28 lines (25 loc) · 843 Bytes
/
Copy pathSoundManager.java
File metadata and controls
28 lines (25 loc) · 843 Bytes
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
package game_template;
import java.io.BufferedInputStream;
import java.io.InputStream;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public class SoundManager {
public SoundManager() {
//
}
// https://stackoverflow.com/questions/15526255/best-way-to-get-sound-on-button-press-for-a-java-calculator
// https://stackoverflow.com/questions/12917297/using-a-url-with-audioinputstream
public void play(String file) {
try {
InputStream is = ClassLoader.getSystemResource(file).openStream();
BufferedInputStream bis = new BufferedInputStream(is);
AudioInputStream ais = AudioSystem.getAudioInputStream(bis);
Clip clip = AudioSystem.getClip();
clip.open(ais);
clip.start();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}