2
\$\begingroup\$

I have a LibGdx game, where the user taps on the screen, and a Sound will play. When the user taps on the screen, a texture will "fire" another texture as a missile, and can hit a target, when this target "dies", another sound will play.

I use these two Sounds often in the game, Shooting Sound, and Hit Sound. When I load the game, everything works fine - the sound doesn't get an error when I load multiple instances of the Sound/Textures on level 1 - so it doesn't seem to be related to memory, because this error can appear when I use a lot less instances of sound/textures, but when I have loaded the "game screen" more times, it starts to appear.

The error is only related to two sound-files, one when the user taps the screen, and the other when the "target" dies. So it does only happen when I load Sounds a lot of times.

The problem only occurs when I go to higher levels, and load the game screen more times. I load the same game screen over and over, only with more instances of "targets".

Error:

W/SoundPool﹕ Sample was not loaded. Waiting for 30ms.
W/AudioTrack﹕ AUDIO_OUTPUT_FLAG_FAST denied by client
com.mygdx.game.android E/AudioTrack﹕ AudioFlinger could not     create track,     status: -12
com.mygdx.game.android E/SoundPool﹕ Error creating AudioTrack

I load my Sound like this:

 Sound playDeadSound;
 public void playDeadSound(){
 soundDead = Gdx.audio.newSound(Gdx.files.internal("dead.ogg"));
 soundDead.play();
 soundDead.dispose();
  }

Things I have tried

  • To instantiate the Sound file in the create method, and then use the same object over and over.
  • I have tried to call soundDead.resume(); instead of play(); to load the sound.
  • I have tried to re-sample my audio to 48KHz, and to the lowest (which was around 5-8KHz IIRC).
  • I have tried to start out with 40 targets, and I get no errors, and I have tried to start out with 5 targets, and errors start appearing when I go a few levels higher and the targets are 10-15.
  • I have tried to remove all other sounds except one of the Sounds that creates the error (Shooting Sound/Hit Sound).
  • I have tried to stop and then dispose the sound, and then create it when I call it.
  • I have tried to have 2 sound files, and then call them one after another, so they doesn't get used at the same time.

The files are 5KB big each, and around 1 second long. Any ideas on what could cause this error? Thanks!

Solution:

The problem was that I reloaded the Sound over and over when I started a new level, hence why the problem never occured when I was at level 1. The problem got fixed when I used the same sound during the whole app's lifetime. A big thanks to Alesegdia for coming up with the answer.

\$\endgroup\$
3
\$\begingroup\$

If you just dispose the sound, you're trying to play something unexistent.

Also, this is a bad practice because you're constantly creating and destroying a sound when playing it, and that's not needed. Try to get the dispose to the end of the game, place your sound somewhere available and just play or stop it when needed.

This is some sketching code, not saying that's the best designed way.

public class Sfx {
    private static Sound deadSound;
    private static long deadID;
    public static void Initialize() {
        deadSound = Gdx.audio.newSound(Gdx.files.internal("deadSound.wav"));
        deadID = deadSound.play(0);
    }
    public static void PlayDeadSound() {
        deadSound.stop(deadID);
        deadID = deadSound.play(1f);
    }
}

...

// at Game::create(), make sure it's called just **ONCE**
Sfx.Initialize();

// somewhere at your game
Sfx.PlayDeadSound();
\$\endgroup\$
  • \$\begingroup\$ Thanks for your reply, but that's what I meant with: "To instantiate the Sound file in the create method, and then use the same object over and over.". I have already tried that, and then the problem occurs more frequently, hence the other way of doing it. \$\endgroup\$ – John Pulple Sep 12 '15 at 6:23
  • \$\begingroup\$ You are sure you are not reloading your assets when you reset the level, right? \$\endgroup\$ – alesegdia Sep 12 '15 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.