18

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?

leech
  • 8,293
  • 7
  • 62
  • 78

3 Answers3

8

Not without submitting a patch to libgdx as far as I know the underlying Sound Backends for both OpenAL and Android don't even track the information internally, though the Music API has an isPlaying() function and getPosition() function as per the documentation.

Appleman1234
  • 15,946
  • 45
  • 67
  • 2
    This was brought up in the latest libGDX hangout too (confirming that there is no API ATM): http://www.youtube.com/watch?v=Ppr7QPeMlMo. I believe the work-around mentioned in the video is knowing how long your audio runs... – P.T. Jul 29 '12 at 02:59
  • 1
    I resolved on two things, making the sounds more acceptable to play overlapped, and saving the last play time and play length. I still love libgdx notwithstanding. – leech Jul 30 '12 at 17:09
  • @P.T. That's not a workaround for being able to 'tell when the sound is finished'...it's the opposite. That's the only thing you can do if you don't have that function at all, which is the case here. – Hasen Jan 12 '21 at 11:18
0

just set this

sound.setLooping(false);

this way it will not run again and again.

and to check whether sound is playing or not do this.

make a boolean variable

boolean soundplaying;

in render method do this

if(sound.isPlaying()){
soundplaying =true
}

and make a log

gdx.app.log("","sound"+soundplaying);
sandeep kundliya
  • 963
  • 8
  • 13
  • I think this answer is correct. Just instead of `Sound` class use `Music` class and it got all you want - use `isPlaying()` or `setOnCompletionListener(..)` and `OnCompletionListener`. – monnef Mar 31 '14 at 20:07
-1

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.

hughjdavey
  • 1,122
  • 2
  • 9
  • 17
  • How does this detect if the sound has finished playing? Like if I play a 2 second long dog bark sound, it plays for 2 seconds and then finishes - how does your code indicate that the dog has finished barking? – Hasen Jan 12 '21 at 11:15