-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundPlay.java
More file actions
44 lines (38 loc) · 1.31 KB
/
Copy pathSoundPlay.java
File metadata and controls
44 lines (38 loc) · 1.31 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
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.io.File;
public class SoundPlay implements Runnable
{
private static String soundName = "resources/new_msg.wav";
private String dynamicSound;
public static String getSoundName() { return soundName; }
public static void setSoundName(final String soundName) { SoundPlay.soundName = soundName; }
public SoundPlay() { }
public SoundPlay(final String dynamicSound) { this.dynamicSound = dynamicSound; }
@Override
public void run()
{
File sound;
try{
if(dynamicSound != null && !dynamicSound.isEmpty())
{
sound = new File(dynamicSound).getAbsoluteFile();
}
else
{
sound = new File(soundName).getAbsoluteFile();
}
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(sound);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
Thread.sleep(clip.getMicrosecondLength()/1000);
}
catch (Exception ex)
{
ex.printStackTrace();
System.err.println("-> Sound Play Failed: " + ex.getMessage());
}
}
}