-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoundHandler.java
72 lines (67 loc) · 1.61 KB
/
SoundHandler.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
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
62
63
64
65
66
67
68
69
70
71
72
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
import javax.swing.*;
/**
the sound handler
@author Jack Basinet and Kush Banker
@version 11.25.19
*/
class SoundHandler
{
private static Clip clip;
public static boolean mute;
public static void playSong(String song)
{
if(!mute)
{
try {
if (clip != null) { clip.stop(); }
File soundFile = new File(song);
AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
}
public static void playSound(String sound)
{
if(!mute)
{
try {
File soundFile = new File(sound);
AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
}
public static void mute()
{
if(mute)
{
mute = false;
SoundHandler.playSong("sound/New-Hope.wav");
}
else
{
mute = true;
clip.stop();
}
}
}