libgdx在收集苹果时收听sound.play()时滞后

pet*_*ter 3 java audio android libgdx

我正在做一个简单的libgdx游戏.当我使用sound.play()在android 4.0上编辑这个bug apear 时,我有滞后(游戏停止0.5秒) 2.3一切正常.

方法.我用这段代码播放声音:

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

        Assets.eatSound.play();

}
Run Code Online (Sandbox Code Playgroud)

我用这种方法加载声音:

 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;
 }
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?或者我该怎么做才能解决这个滞后问题?

编辑:

我刚刚尝试用sound.play()做线程,但它也不起作用:

    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();
Run Code Online (Sandbox Code Playgroud)

我的声音资产类看起来像这样,但我仍然有声音滞后.包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

}
}
Run Code Online (Sandbox Code Playgroud)

小智 5

我有同样的问题.这是因为我的.mp3文件太短了.我的长度是0.167秒.我用Audacity添加了1.2秒的沉默,它解决了这个问题.