-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundEffect.java
More file actions
executable file
·130 lines (109 loc) · 3.51 KB
/
Copy pathSoundEffect.java
File metadata and controls
executable file
·130 lines (109 loc) · 3.51 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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.lang.*;
import java.util.*;
import javax.sound.sampled.*;
import java.net.URL;
import java.io.File;
import java.io.IOException;
public enum SoundEffect {
PING("sounds/17__anton__glass-a-pp.wav"),
PING2("sounds/17__anton__glass-a-pp.wav"),
PING3("sounds/17__anton__glass-a-pp.wav"),
//PING4("sounds/17__anton__glass-a-pp.wav"),
//PING5("sounds/17__anton__glass-a-pp.wav"),
BREAK("sounds/glass-break.wav"),
BUZZ("sounds/bumblebee.wav"),
BELL("sounds/small_ring.wav"),
FAIL("sounds/price-is-right-fail.wav");
// Nested class for specifying volume
public static enum Volume {
MUTE, LOW, MEDIUM, HIGH
}
public static SoundEffect getPing(int i)
{
SoundEffect ping;
switch(i)
{
//case 4:
//return PING5;
//case 3:
//return PING4;
case 2:
return PING3;
case 1:
return PING2;
case 0:
default:
return PING;
}
}
public static Volume volume = Volume.LOW;
// Each sound effect has its own clip, loaded with its own sound file.
private Clip clip;
// Constructor to construct each element of the enum with its own sound file.
SoundEffect(String soundFileName) {
try {
// Use URL (instead of File) to read from disk and JAR.
URL url = this.getClass().getClassLoader().getResource(soundFileName);
// Set up an audio input stream piped from the sound file.
AudioInputStream audioInputStream = null;
if(null == url)
{
File f = new File(soundFileName);
audioInputStream = AudioSystem.getAudioInputStream(f);
}
else
{
audioInputStream = AudioSystem.getAudioInputStream(url);
}
// Get a clip resource.
clip = AudioSystem.getClip();
// Open audio clip and load samples from the audio input stream.
clip.open(audioInputStream);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
// Play or Re-play the sound e/ffect from the beginning, by rewinding.
public void play() {
stop();
if (volume != Volume.MUTE) {
//if (clip.isRunning())
// clip.stop(); // Stop the player if it is still running
//clip.setFramePosition(0);
clip.start(); // Start playing
}
}
public void pause()
{
if (clip.isRunning())
clip.stop();
}
public void stop()
{
if (clip.isRunning())
clip.stop();
clip.setFramePosition(0);
}
public void playIfNotPlaying()
{
if (volume != Volume.MUTE) {
if (clip.isRunning())
return; // Stop the player if it is still running
clip.setFramePosition(0); // rewind to the beginning
clip.start(); // Start playing
}
}
// Optional static method to pre-load all the sound files.
static void init() {
values(); // calls the constructor for all the elements
}
}