LIBGDX: How can i tell when a sound has finished playing?

Viewed 7184

The Sound API seems to be missing a function to indicate that a sound is finished playing. Is there some other way of finding out if the sound is done?

3 Answers

You can track this by storing the sound instance id that e.g. play() and loop() return. Example code:

private Sound sound;
private Long soundId;

...

public void startSound()  {
    if (soundId != null) {
        return;
    }
    soundId = sound.loop();   // or sound.play()
}

public void stopSound() {
    sound.stop(soundId);
    soundId = null;
}

If you didn't want to have to call stopSound() from client code, you could just call it from startSound() instead of the return, to ensure any previous sound is stopped.

Related