Audio Streaming in Lagrange for Android
π
2026-03-15
π· Lagrange, Android
I've been working on improving Lagrange's Android port this week. It has been a bit of a learning experience β of the major computing platforms, Android is probably the least familiar to me.
Prior to the Beta 46 build, audio support has been somewhat broken on Android. The platform-independent Vorbis decoder that plays audio through SDL has been limping along while other codecs haven't worked that well, if at all. However, now in Beta 46/47, there are major changes:
- The system attempts to use native Android decoders for all audio formats. This means you'll get the best possible performance and smallest hit on battery life.
- Playback begins as soon as the native media player is able to start its decoding, meaning if the content supports streamed decoding, you can listen while downloading.
- Audio playback can continue when the app goes to the background. Android is actually looking out for you here by making the app jump through a bunch of hoops. One needs to request special permissions to be able to run anything while in the background, and you specifically have to declare why you are running (e.g., media playback). You are also required to put up a system notification letting the user know that there is something going on in the background ("Playing audio"), which means requesting permission for the app to show notifications.
- It gets even better: if the device is locked (screen turns off), the CPU and radios by default want to go to sleep to preserve power. The app needs to request keeping the device in a higher power state during the media playback. This is especially important for the streaming, since that involves both radios and the CPU threads handling the data. If the app was a typical Android app using Google's high-level media APIs, most of this stuff would be handled automatically, but I'm doing the audio playback on a lower level as we are going Gemini requests to fetch the data, and handing the data over to Android via streaming memory buffers.
- The Ogg container format gets a (dis)honorable mention: Android's media player does not support streamed decoding of Ogg content, because it wants to first know the duration of the content by skipping to the end. Therefore, I had to resort to using the old stb_vorbis & SDL audio solution for Ogg Vorbis. (Ogg Opus still doesn't stream.) On the plus side, this code path should also now fully support background playback and streaming.
Long story short, you can now do both background audio playback and background audio streaming in the Android port. This is a step above the current iOS implementation that doesn't barely supports streaming playback at all.
You can find the Android Beta builds in the recently rebuilt Lagrange F-Droid repository (now using fdroid v2 format):
CC-BY-SA 4.0