Libgdx: Lags in soundtrack looping

你离开我真会死。 提交于 2019-12-02 06:09:51

The problem is the way libGDX handles Music. I will quote a badlogic post on GitHub issue 1654.

the Android situation is a bit more complicated and sad. On Android we use the system facilities to playback audio, namely MediaPlayer. This pieces of Android software uses device dependend drivers under the hood (audio drivers, custom codec implementations, etc.). This means that we are at the mercy of hardware vendors like Samsung and their driver implementations.

The problem is not bound only to libGDX, it is Android issue 18756.

Solution

Your soundtrack is short and small in term of memory size so using libGDX sound is actually better in this case and it is free of this gap bug.

Music -> long and big files, not loaded into memory

Sound -> short and small files, loaded into memory

Use Sound class and loop it. Example:

long id;
...
public void create() {
    music = Gdx.audio.newSound(Gdx.files.internal("soundtrack.ogg"));
    id = music.loop(); //Sound may not be ready here!
}

public void render() {
    if(id == -1)
        id = music.loop();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!