Member-only story

Background audio in Flutter with Audio Service and Just Audio

Suragch
35 min readJul 17, 2021

--

A full step-by-step tutorial

Article updated on March 18, 2023.

If you’re only playing short audio clips in your app, then generally a simple audio player plugin will work. However, for longer audio forms like music or podcasts, this is often insufficient. Let’s say you start playing a podcast but then leave this app to check your fitness app. What does the podcast app do? It could stop the audio as soon as you leave, but you want to keep listening, so that’s not a good option. On the other hand, if the app allows the podcast to keep playing in the background, imagine the second scenario: You’re happily exercising and listening to the podcast when all of a sudden someone knocks on the door. Where’s the pause button? How do you stop the podcast!? Knock, knock, knock! Argh! There’s no stop button! You finally navigate back to the podcast app and find a way to stop the audio. How annoying!

I should know since I made an app like that once.

The Flutter audio_service plugin solves the problems described above by allowing users to listen to audio outside of the app and also by giving users access to system-wide audio control buttons. On Android these buttons are located in the notification drawer and the lock screen. On iOS they are located in the control center and notification center.

Audio controls in the Android Notification Drawer and iOS Control Center (image sources: Android Police and iOS docs)

Internally audio_service does all the work to interface with Android, iOS, and other platforms so that you don’t need to worry about platform-related details (though some features are platform specific).

In this tutorial, I’ll walk you through the steps of setting up an audio player that supports background audio. Audio Service isn’t an audio player; it’s just an interface to the system audio controls. For the audio player itself, you’ll need another plugin. In this tutorial I’ll use just_audio, which is also by Ryan Heise, the author of audio_service. If you aren’t familiar with just_audio, I recommend you check out my previous tutorials on the topic, especially the third one:

--

--

Suragch
Suragch

Written by Suragch

Flutter and Dart developer. Twitter: @suragch1, Email: suragch@suragch.dev

Responses (21)

Write a response