-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundClip.java
More file actions
executable file
·43 lines (37 loc) · 1.17 KB
/
SoundClip.java
File metadata and controls
executable file
·43 lines (37 loc) · 1.17 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
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
import javax.swing.*;
/*
* HOW TO USE: SoundClip yoursound = new SoundClip("YourSound.wav");
*/
public class SoundClip {//extends JFrame {
// Constructor
public SoundClip(String file) {
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.setTitle("Test Sound Clip");
//this.setSize(300, 200);
//this.setVisible(true);
try {
// Open an audio input stream.
URL url = this.getClass().getClassLoader().getResource(file);
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
// Get a sound clip resource.
Clip clip = AudioSystem.getClip();
// Open audio clip and load samples from the audio input stream.
clip.open(audioIn);
clip.start();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
//Test it!
/*
public static void main(String[] args) {
new SoundClip("dicerollsound.wav");
}*/
}