Skip to content

Add a suggestion on looping seamless music#266

Open
Ian2020 wants to merge 1 commit intolibgdx:devfrom
Ian2020:patch-1
Open

Add a suggestion on looping seamless music#266
Ian2020 wants to merge 1 commit intolibgdx:devfrom
Ian2020:patch-1

Conversation

@Ian2020
Copy link
Copy Markdown

@Ian2020 Ian2020 commented May 7, 2026

I learnt this today in the discord - MP3 can introduce a gap in looping music when looped, Ogg format can solve the problem. Thought this was useful in the wiki.

MP3 can introduce a gap in looping music when looped. Ogg is preferable.
@obigu
Copy link
Copy Markdown
Contributor

obigu commented May 7, 2026

In my experience Ogg also introduces a gap when looping it using libGDX Audio default backend, especially on Android.

Currently, afaik, the only working solution to avoid this issue is to use gdx-miniaudio (https://github.com/rednblackgames/gdx-miniaudio).

If we add a recommendation in the wiki regarding this issue, I think gdx-miniaudio should be mentioned.

EDIT: I see it's already recommended here https://libgdx.com/wiki/audio/audio

Copy link
Copy Markdown
Collaborator

@Frosty-J Frosty-J left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been wanting to update those music and sound effects pages but scope creep got the better of me.

Ogg Vorbis logo

My advice would be to not use MP3 unless your music came in that format and you don't wish to re-encode. It has full compatibility across libGDX platforms (iOS was the problem child), and that's all. Ogg doesn't have the gap issue in the codec itself (mileage will vary by platform, e.g. Music on Android will still add a really big gap) and is higher fidelity for the same bitrate, plus the desktop module is slower than it really should be at decoding and seeking MP3. Most importantly of all, the Vorbis logo looks so cool and is Creative Commons.

Having a paragraph about gapless looping isn't unreasonable, given it's such a common requirement in video games. gdx-miniaudio is third-party, but in all honesty libGDX's own audio handling is just a thin wrapper designed around the lowest common denominator (Android, maybe even the very first version of it) and thus isn't the best. TuningFork could also be worth considering for desktop-only games.

My prior experience was desktop (LWJGL) looped fine with Ogg. I think using MediaPlayer#setNextMediaPlayer on Android worked (wouldn't be surprised if results vary by device, though) but I didn't propose making libGDX use that as then either every Music instance would use two MediaPlayers (adding some degree of runtime weight) or a gapless looping overload would have to be added (messy, especially if only applicable to one platform).

For web games, you can exclude your music from the preload filter and do not use AssetManager, then Music just streams from the server. This allows you to loop the music before encoding the file, to be an hour long or however much you think is longer than the user will hear during gameplay, without increasing load times. Upload size isn't too bad if using (HE-)AAC or Opus (the former supports iOS < 18.4), and the largish audio only has to be uploaded once if only uploading build deltas (e.g. itch.io butler). A stupid hack for sure, when I believe this could be fixed at the WebAudio level (and also fixing games causing playback controls to appear in notification area), but hey, it works (unless internet connection is lost).

float position = music.getPosition(); // returns the playback position in seconds
```

Music that should loop seamlessly may work better in the Ogg format due to the limitations of MP3 (see 'Why cant MP3 files be seamlessly spliced together?' at [LAME Technical FAQ](https://lame.sourceforge.io/tech-FAQ.txt)).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clearly I will get carried away again. I approve of this addition, as it's better than having nothing at all!

@obigu
Copy link
Copy Markdown
Contributor

obigu commented May 7, 2026

I've been wanting to update those music and sound effects pages but scope creep got the better of me.

Ogg Vorbis logo

My advice would be to not use MP3 unless your music came in that format and you don't wish to re-encode. It has full compatibility across libGDX platforms (iOS was the problem child), and that's all. Ogg doesn't have the gap issue in the codec itself (mileage will vary by platform, e.g. Music on Android will still add a really big gap) and is higher fidelity for the same bitrate, plus the desktop module is slower than it really should be at decoding and seeking MP3. Most importantly of all, the Vorbis logo looks so cool and is Creative Commons.

Having a paragraph about gapless looping isn't unreasonable, given it's such a common requirement in video games. gdx-miniaudio is third-party, but in all honesty libGDX's own audio handling is just a thin wrapper designed around the lowest common denominator (Android, maybe even the very first version of it) and thus isn't the best. TuningFork could also be worth considering for desktop-only games.

My prior experience was desktop (LWJGL) looped fine with Ogg. I think using MediaPlayer#setNextMediaPlayer on Android worked (wouldn't be surprised if results vary by device, though) but I didn't propose making libGDX use that as then either every Music instance would use two MediaPlayers (adding some degree of runtime weight) or a gapless looping overload would have to be added (messy, especially if only applicable to one platform).

For web games, you can exclude your music from the preload filter and do not use AssetManager, then Music just streams from the server. This allows you to loop the music before encoding the file, to be an hour long or however much you think is longer than the user will hear during gameplay, without increasing load times. Upload size isn't too bad if using (HE-)AAC or Opus (the former supports iOS < 18.4), and the largish audio only has to be uploaded once if only uploading build deltas (e.g. itch.io butler). A stupid hack for sure, when I believe this could be fixed at the WebAudio level (and also fixing games causing playback controls to appear in notification area), but hey, it works (unless internet connection is lost).

I don't object recommending Ogg for Desktop and GWT to fix the gap, just needs to be specified it only applies to those backends. iOS doesn't support Ogg and on Android, as you acknowledge there's still a large gap. For Android and iOS I'd add a recommendation to use gdx-miniaudio so that the recommendation covers all libGDX backends.

@Ian2020
Copy link
Copy Markdown
Author

Ian2020 commented May 8, 2026

Thanks both for your feedback. Though I'd draft a new paragraph here rather than make multiple commits to the PR, so it's at the end of this comment.

Some notes on the feedback:

  • I can see the situation is more complicated then just recommending Ogg, so each platform ought to get some explanation.
  • I've held back from including the Android dual MediaPlayer workaround - if it's not guaranteed to work on every device it's probably not ready to be recommended to all.
  • Perhaps TuningFork could be added to the alternatives section of the Audio page in its own PR perhaps? Would it improve seamless play for MP3s or is switching to Ogg still the best improvement for Desktop?

Anyway, here is the rewritten paragraph on seamless music:


If you have music that needs to loop seamlessly be aware that whilst the MP3 format is compatible across all platforms it has technical limitations preventing fully seamless play (see 'Why cant MP3 files be seamlessly spliced together?' at LAME Technical FAQ). The approach to solving this will vary depending on the platforms you need to target:

  • For desktop and web only the Ogg format may work better as it avoids the gap issue of MP3 and (as a bonus) has higher fidelity for the same bitrates. The format is not supported on iOS however and on Android there will still be some gap due to current limitations of the LibGDX Audio module.
  • If targeting iOS and Android look at using a third-party cross-platform audio backend alternative, in particular gdx-miniaudio.
  • For web only you could also consider streaming pre-looped music directly from your server. Do this by excluding the music from the preload filter and not using the AssetManager. This allows you to loop music for as long as needed, without increasing game load times.

@obigu
Copy link
Copy Markdown
Contributor

obigu commented May 8, 2026

Looks good to me! Considering it's several paragraphs and goes quite deep I would add it as a minor subsection with it own title (using markdown ## Seamless Music) to improve readability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants