-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundEffect.java
122 lines (107 loc) · 2.74 KB
/
SoundEffect.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
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
package bettycrocker;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
/**
* A SoundEffect is used to play a short sound clip typically short enough
* to store completely in memory. A SoundEffect can be played at any time
* by using the 'play' method.
*
* To create a SoundEffect, simply use the constructor that takes the
* name of the sound file (using a relative path).
*
* In addition, the volume and looping attributes
* can be set by the user.
*
* @author John
*
*/
public class SoundEffect {
private File soundFile;
private Clip clip;
private AudioInputStream audioStream;
/**
* Creates a SoundEffect from the supplied file path.
* @param fileName
*/
public SoundEffect(String fileName) {
soundFile = new File(fileName);
}
/**
* Plays the SoundEffect once.
*/
public void play() {
play(1);
}
/**
* Plays the sound effect a user defined
* amount of times.
* @param times
*/
public void play(int times) {
try {
audioStream = AudioSystem.getAudioInputStream(soundFile);
clip = AudioSystem.getClip();
clip.open(audioStream);
clip.addLineListener( new LineListener() {
@Override
public void update(LineEvent evt) {
if (evt.getType() == LineEvent.Type.STOP) {
evt.getLine().close();
}
}
});
if (times != Clip.LOOP_CONTINUOUSLY) {
clip.loop(times - 1); //The Clip method for looping states 0 will play it once)
} else {
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
} catch (UnsupportedAudioFileException e) {
System.out.println("Unsupported audio file.");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Problem reading the audio file.");
e.printStackTrace();
} catch (LineUnavailableException e) {
System.out.println("Dataline is not available.");
e.printStackTrace();
}
}
/**
* Plays the SoundEffect indefinitely.
*/
public void loop() {
play(Clip.LOOP_CONTINUOUSLY);
}
/**
* Stops playing the SoundEffect
*/
public void stop() {
if (clip.isRunning()) {
clip.stop();
}
}
/**
* Returns the sound file associated with this
* SoundEffect.
* @return
*/
public File getSoundFile() {
return soundFile;
}
/**
* Sets the volume of the SoundEffect.
* @param newVolume
*/
public void setVolume(float newVolume) {
FloatControl volume = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(newVolume);
}
}