Hi, I used this little class in a recent project to “Queue” or rather play 1 sound effect after another with a delay.
It worked quite nice, someone might find a use for it, very simple. It can be built on, to remove tracks and swap track order but heh I just made this quickly.
public class SoundSequencer {
/** A list of tracks to play in a sequence */
Array<Sound> sounds = new Array<Sound>();
/** The index to play */
int index = 0;
/** The delay between tracks */
double delay;
/** The last time a track was played */
double lastPlayed;
/** Add a sound to the sequencer */
public void addSound(String path) {
Sound sound = Gdx.audio.newSound(Gdx.files.internal(path));
if (sounds.contains(sound, true))
return;
sounds.add(sound);
}
/**
* Play the sound sequence with a given delay
*
* @param volume 0 - 1
* @param delay
* in seconds
*/
public void play(float volume, double delay) {
if (TimeUtils.nanoTime() - lastPlayed > delay / 1000000000) {
play(volume);
}
}
/**
* Play the sound track
*
* @param volume 0 - 1
*/
public void play(float volume) {
if (sounds.get(index) != null) {
sounds.get(index).play(volume);
lastPlayed = TimeUtils.nanoTime();
}
nextTrack();
}
/** Move to the next track on the play list */
private void nextTrack() {
if (index + 1 > sounds.size - 1) {
index = 0;
} else {
index += 1;
}
}
}