How to play a song or podcast from a URL
Updated for Flutter 2.5 and Just Audio 0.9.15 on November 2, 2021
In my last article, Playing short audio clips in Flutter with Just Audio, I demonstrated how to set up Just Audio and play audio clips that only last a few seconds. If you’re new to the Just Audio plugin, I recommend you go through that tutorial first.
This tutorial will demonstrate how to play long-form audio like songs, podcasts, or lectures. While it’s completely possible to play these from files stored on the user’s device, a more common scenario is to stream them based on a URL link, so that’s the direction this tutorial will go. Most of what you learn will also apply to playing a local file, though.
In order to keep this tutorial focused, it will only cover basic topics like playing, pausing, buffering, and seeking in a single audio file. For a full-fledged app, you would also want to implement playlists and background audio, but those topics will have to wait for another tutorial.
If you get lost along the way you can refer to the project source code here.
Setup
Rather than describe the detailed setup process here, I’ll just briefly mention the steps. Refer to the documentation and my previous tutorial for more details.
Flutter setup
Start a new project.
Make sure that you’re using null-safe Dart 2.12 or higher in pubspec.yaml (not required for Just Audio but the code in this tutorial will use null safety):
environment:
sdk: ">=2.12.0 <3.0.0"
Just Audio setup
Add just_audio to pubspec.yaml:
dependencies:
just_audio: ^0.9.15
If you’re creating a macOS app, then you need to allow the app to access the internet. The directions are here.
This tutorial will play audio from an HTTPS link, but if you want to substitute an HTTP link then you should enable clear text. The directions are here.