No Sound in Android Application with Libgdx

一个人想着一个人 提交于 2019-11-29 08:08:06

Sometimes the sound doesn't play the same frame it was loaded, try waiting one frame. Or playing it in another class.

Reference: Sound not playing after Loading

Load your sound and verify if it is loaded before playing. I had the same problem and this works fine.

public void create(){
    assetManager = new AssetManager();
    assetManager.load("path/to/sound.mp3", Sound.class);
    assetManager.finishLoading(); //Important!
}

//The load must be tested within the loop;
public void render(){
    if (assetManager.isLoaded("path/to/sound.mp3")){
        sound = assetManager.get("path/to/sound.mp3", Sound.class);
        sound.play();
    }
}
ArcheNoah

The following code worked well on the device:

public class LevelDesigner {

   private AssetManager assetManager;

   public LevelDesinger {
      assetManager = new AssetManager();
      assetManager.load("data/sounds/loop.ogg", Music.class);
   }

   public boolean startMusic() {

      if(assetManager.isLoaded("data/sounds/loop.ogg")) {
          Music music = assetManager.get("data/sounds/loop.ogg", Music.class);
          music.play();
          music.setLooping(true);
      }else {
          System.out.println("not loaded yet");
          return false;
      }
      return true;
   }
}

Explananation: It is important to check if the sound has been loaded. If it isn't loaded an your try to play it doesn't through a exception so this is hard to find for a beginner ;)...

Check that your mp3, ogg or wav isn't too short or too long! Sounds crazy but I found out about this issue here: http://github.com/libgdx/libgdx/issues/1366 and I tried a sound that I knew worked from another game and that worked.

My working sound, a piano keypress can be downloaded here:

http://www.martincapodici.com/wp-content/uploads/2014/08/a4.ogg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!