just_audio in Flutter, gotchas and patterns
flutterjust_audioaudioandroidriverpod
just_audio (0.10 at the time of writing) is the standard Flutter audio player package,
with just_audio_background adding the Android/iOS media notification and lock-screen
controls. These are the traps found while building a streaming music player (~/dev/music,
Flutter 3.41, Riverpod 3) in July 2026.
API gotchas
- Never
await player.play(). The returned future completes when playback ends, not when it starts. Awaiting it froze loading spinners and blocked queue filling until the track finished. Call it without awaiting and react toplayerStateStream. ConcatenatingAudioSourceis deprecated. Use the playlist methods on the player itself:player.setAudioSources(...),addAudioSources(...),removeAudioSourceAt(...).- The audible track is
player.currentIndex. When acting on "the current song" (thumbs-down, skip, blacklist) look the track up byplayer.currentIndex. A derived "current track" provider that falls back to the queue head will point at the wrong track while sources are being swapped. - just_audio on web cannot send custom headers, so streams that need auth must use a token in the URL query string.
- On web the player's
<audio>element is not attached to the DOM, so automated tests cannot inspect it. Verify playback indirectly, for example by watching a persisted position value advance.
Android setup (Android 14+)
just_audio_background needs all three, or the foreground service crashes or never
starts:
- <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK"/>
- android:foregroundServiceType="mediaPlayback" on the audio service in the manifest
- MainActivity extends AudioServiceActivity instead of FlutterActivity
just_audio_background is native-only; guard its init so web builds skip it.
Session resume pattern
Persist queue, trackId, and positionMs in shared preferences and restore them
paused on launch. Save position every ~10 s while playing and on the playing to
paused transition only. A naive "save whenever not playing" listener fires during the
restore load (position 0, not playing) and overwrites the saved position with zero. This
was caught only by an end-to-end headless Chrome check.
Related package notes
file_picker11:FilePicker.pickFiles(...)is static now; the oldFilePicker.platform.pickFilesform is gone.- Riverpod 3 moved
ChangeNotifierProvidertopackage:flutter_riverpod/legacy.dart. - Tag readers like
audio_metadata_readerare native-only; use a conditional import.
See also
- Streaming public-domain music from the Internet Archive
- Flutter web (renderers, SEO, performance, when not to use it)
- Headless Chrome testing gotchas