// PLAYING SOUND in JAVA
//Copy and paste the code below to a separate class (SoundEffects class) inside your project.
//In order to make the sound work,
//just put the code: SoundEffects.song1.play();
//In order to make the sound stop type the code: SoundEffects.song1.halt();
//This only works for .wav or .aiff audio files, so might
//have to convert in your audio software.
//Drag your audio file into the src folder inside the Project.
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
public enum SoundEffects {
song1("gong.wav"),
song2("intro.aiff");
public static enum Volume {
MUTE, LOW, MEDIUM, HIGH
}
public static Volume volume = Volume.LOW;
private Clip clip;
SoundEffects(String soundFileName) {
try {
URL url = this.getClass().getClassLoader().getResource(soundFileName);
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
clip = AudioSystem.getClip();
clip.open(audioInputStream);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
public void play() {
if (volume != Volume.MUTE) {
if (clip.isRunning())
clip.stop();
clip.setFramePosition(0);
clip.start();
}
}
public void BMusic() {
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
public void halt() {
if (clip.isRunning())
clip.stop();
}
static void init() {
values();
}
}