Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libgdx lag when sound.play() while collecting apple

I'am doing simple libgdx game. I have lag (game stop for 0. 5 sec) when i use sound.play() edit this bug apear on android 4.0 on 2.3 everything is running fine.

method. I play sound by this code:

if(CollisionDetector.detect(touchArea, hoodie.getTouchArea())){
        GameScreen.totalScore++;
        setPosition();
        System.out.println("played");

        Assets.eatSound.play();

}

And i use this method to load sound:

 static long waitForLoadCompleted(Sound sound,float volume) {
        long id;
        while ((id = sound.play(volume)) == -1) {
            long t = TimeUtils.nanoTime();
            while (TimeUtils.nanoTime() - t < 100000000);
        }
        return id;
 }

What am i doing wrong? Or what can i do to fix this lag ?

edit:

I have just tried to do thread with sound.play() but it also doesn't work:

    new Thread(new Runnable() {
           @Override
           public void run() {
              // do something important here, asynchronously to the rendering thread

              // post a Runnable to the rendering thread that processes the result
              Gdx.app.postRunnable(new Runnable() {
                 @Override
                 public void run() {
                    // process the result, e.g. add it to an Array<Result> field of the ApplicationListener.
                eatSound2.play();
                 }
              });
           }
        }).start();

My Sound asset class looks like this but i still have lag with sound. package com.redHoodie;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.utils.Disposable;

public class SoundEffect implements Disposable {
   private static final int WaitLimit = 1000;
   private static final int ThrottleMs = 100;
    Sound eatSound;
    Sound endSound;

    public SoundEffect(){
        eatSound = Gdx.audio.newSound(Gdx.files.internal("eatSound.ogg"));
        endSound = Gdx.audio.newSound(Gdx.files.internal("sadend.wav"));

        checkedPlay(eatSound);
    }

   protected long checkedPlay (Sound sound) {
      return checkedPlay(sound, 1);
   }

   protected long checkedLoop (Sound sound) {
      return checkedLoop(sound, 1);
   }

   protected long checkedPlay (Sound sound, float volume) {
      int waitCounter = 0;
      long soundId = 0;

      boolean ready = false;
      while (!ready && waitCounter < WaitLimit) {
         soundId = sound.play(volume);
         ready = (soundId != 0);
         waitCounter++;
         try {
            Thread.sleep(ThrottleMs);
         } catch (InterruptedException e) {
         }
      }

      return soundId;
   }

   protected long checkedLoop (Sound sound, float volume) {
      int waitCounter = 0;
      long soundId = 0;

      boolean ready = false;
      while (!ready && waitCounter < WaitLimit) {
         soundId = sound.loop(volume);
         ready = (soundId != 0);
         waitCounter++;
         try {
            Thread.sleep(ThrottleMs);
         } catch (InterruptedException e) {
         }
      }

      return soundId;
   }

@Override
public void dispose() {
    // TODO Auto-generated method stub

}
}
like image 588
peterSweter Avatar asked Jul 27 '14 18:07

peterSweter


1 Answers

I had the same problem. It was because my .mp3 file was too short. Mine was 0.167 seconds long. I added 1.2 seconds of silence with Audacity, and it fixed the problem.

like image 157
Baguette Avatar answered Sep 20 '22 07:09

Baguette