Libgdx:音轨循环滞后

2022-01-12 00:00:00 android java libgdx

我在 libgdx 中循环播放音乐时遇到问题.我知道以前有一个类似的话题,但它根本没有帮助我.问题是当你进入我的游戏的主菜单时(link) 雨声循环播放.不幸的是,每场比赛之间都有片刻的沉默,我不知道为什么——你可以下载游戏看看我的意思.我已经在使用 .ogg 格式,所以我在这里找到的其他主题的解决方案并没有真正的帮助.

I'm having trouble with music looping in libgdx. I know a simmilair topic was here before, but it didn't help me at all. The thing is that when you go to main menu in my game (link) the sound of rain is looped. Unfortunately, there is a short moment of silence between each play and I don't know why - you can download the game and see what I mean. I'm already using .ogg format, so the solution from the other topic I found here didn't really help.

如果我在 Audacity 中循环播放这个声音,效果会很好.

If I play this sound in a loop in Audacity, it works perfectly.

这是我的代码(不过我认为它不会有帮助):

Here is my code (I don't think it'll help, though):

rainSoundtrack = Gdx.audio.newMusic(Gdx.files.internal("soundtrack.ogg"));
rainSoundtrack.setLooping(true);

推荐答案

问题在于 libGDX 处理音乐的方式.我将在 GitHub 问题 1654 上引用一个 badlogic 帖子.

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

Android 的情况有点复杂和悲伤.在 Android 上,我们使用系统工具来播放音频,即 MediaPlayer.这段 Android 软件在底层使用设备相关驱动程序(音频驱动程序、自定义编解码器实现等).这意味着我们受到三星等硬件供应商及其驱动程序实施的摆布.

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.

问题不仅限于 libGDX,它是 Android 问题 18756.

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

解决方案

就内存大小而言,您的配乐很短而且很小,因此在这种情况下使用 libGDX 声音实际上更好,并且没有这个间隙错误.

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

使用 Sound 类并循环它.示例:

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();
}

相关文章