-
Notifications
You must be signed in to change notification settings - Fork 1
/
SoundPlayer.java
57 lines (49 loc) · 2.22 KB
/
SoundPlayer.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
/*
* SoundPlayer.java
* Ingrid and Isabel Crant
* Plays sound effects found in the Jetpack Joyride game. Loads in .wav files and plays it a certain number of times.
*/
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.*;
public class SoundPlayer {
public static final String background = "file:./sounds/background_music.wav";
public static final String coin = "file:./sounds/coin_pickup.wav";
public static final String laserFiring = "file:./sounds/laser_fire_lp.wav";
public static final String laserLoading = "file:./sounds/laser_warning.wav";
public static final String scientistFainting = "file:./sounds/scientist_faint.wav";
public static final String barryWalking = "file:./sounds/foot_step.wav";
public static final String barrySliding = "file:./sounds/fall_slide.wav";
public static final String barryHurt = "file:./sounds/player_hurt_2.wav";
public static final String barryZapped = "file:./sounds/barry_zapped.wav";
public static final String missileWarning = "file:./sounds/missile_warning.wav";
public static final String missileLaunch = "file:./sounds/missile_launch.wav";
// used to play sound effects
// soundToPlay is a string specifying the relative path of the sound effect file
public static void playSoundEffect(String soundToPlay, int loopNum) {
URL soundLocation;
try {
soundLocation = new URL(soundToPlay);
Clip clip = null;
clip = AudioSystem.getClip();
AudioInputStream inputStream;
inputStream = AudioSystem.getAudioInputStream(soundLocation);
clip.open(inputStream);
if(soundToPlay == background){
FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(-3.0f); // reduces volume of background music by 3 decibels.
}
clip.loop(loopNum); // loops the clip loopNum times
clip.start(); // play sound
clip.addLineListener(new LineListener() { // kill sound thread
public void update (LineEvent evt) {
if (evt.getType() == LineEvent.Type.STOP) {
evt.getLine().close();
}
}
});
} catch (LineUnavailableException | UnsupportedAudioFileException | IOException e) {
System.out.println(e.getMessage());
}
}
}