I’m just an amateur games writer but I’d like to know an easy to use way of playing sound in games. I’m looking for something that’s:
- easy of use (most important)
- good performance (important)
- the ability to play mp3’s (preferably)
- volume control (preferably)
- left right control (a nice to have).
When running my current game on a low performance laptop (haswell 2955U, 2GB, no GPU) I find the game itself play fine but the sound is all over the place and eventually seems to turn off. I’m not sure whether this is a caching thing (currently use wav’s sized between 20k and 100k) or whether it’s just too much processing to play the sound as well as the game.
I run the game as a jar and my existing sound code is a bit like this:
SoundEffect.SHOOT.play();
enum SoundEffect {
ALIEN_HIT("alien_hit"),
....
SHOOT("machine_gun");
// Each sound effect has its own clip, loaded with its own sound file.
private Clip clip;
private String name;
private long timeLastSound = System.currentTimeMillis();
// Constructor to construct each element of the enum with its own sound file.
SoundEffect(String file) {
try {
// play sound
name = file;
AudioInputStream inputStream = AudioSystem.getAudioInputStream(this.getClass().getResource(file+".wav"));
AudioFormat format = inputStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
clip = (Clip)AudioSystem.getLine(info);
clip.open(inputStream);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
public void stop() {
if (clip.isRunning()) { clip.stop(); }
}
// Play or Re-play the sound effect from the beginning, by rewinding.
public void play() {
timeLastSound = System.currentTimeMillis();
// Stop the player if it is still running
if (clip.isRunning()) { clip.stop(); }
// play
clip.setFramePosition(0); // rewind to the beginning
clip.start(); // Start playing
}
static void init() {
values();
}
}
Suggestions for a simple to use method of making sounds or how to speed up what I have? would switching to mp3’s help or would I just be trading file size for processing required?
Many thanks
Mike