Sound Effects and .OGG usage?
I am currently working on a video game for my Java class, and I have hit a huge wall. I have no idea how to use audio in Java, and everything I have read online does not seem to work.
I really do not want to resort to using .mp3 files for my audio, but I guess learning how to use them may help me understand how to use .ogg files as well.
But now for the question:
Does anyone have a good method of playing sound effects and music that doesn't sound horrendous?
Re: Sound Effects and .OGG usage?
Hello Good Sir, I have a Java Class that does it all for you!
Here it is:
Code:
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
public enum SoundEffects {
EXPLODE("explode.wav"),
HURT("hurt.wav"),
PICKUP("pickup.wav"),
POWERUP("powerup.wav"),
BREAK("break.wav"),
DISK_ONE("music/disk1.wav");
public static enum Volume {
MUTE, LOW, MEDIUM, HIGH
}
public static Volume volume = Volume.LOW;
private Clip clip;
private boolean isMusic = false;
SoundEffects(String soundFileName) {
if(soundFileName.toLowerCase().startsWith("disk")) {
isMusic = true;
}else {
isMusic = false;
}
try {
URL url = this.getClass().getClassLoader().getResource("sounds/" + 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();
}
}
static void init() {
values();
}
}
The code is also on: [Moderator edit: link removed]
To use it, replace EXPLODE, HURT, etc with sound effects and the locations!