This content provides a tutorial on how to play short audio clips in Flutter with the Just Audio package, including setup, playing audio from assets, and playing different audio clips simultaneously.
Abstract
The content describes a step-by-step tutorial on playing short audio clips in a Flutter app using the Just Audio package. It starts with setting up the UI layout and installing the just_audio package, then moves on to playing audio from assets and creating the audio player. The tutorial also covers playing sounds and playing different audio clips simultaneously. Finally, it discusses playing audio clips from the web and provides additional notes and the full source code for the tutorial.
Bullet points
The tutorial focuses on playing short audio clips in Flutter using the Just Audio package.
Just Audio supports Android, iOS, web, macOS, Windows, and Linux.
Short audio clips are used in various types of apps, such as games, language learning apps, and dictionaries.
The tutorial covers creating the UI layout, installing the just_audio package, and creating the audio player.
The audio player is initialized when the UI loads and disposed of when the app closes.
To play sounds, you first set the audio asset before playing it.
You can play different audio clips simultaneously by creating more than one AudioPlayer instance.
Audio clips can also be played from the web using a URL.
Additional notes include permitting HTTP URLs for Android, iOS, and macOS apps and checking out the documentation for platform-specific configuration.
The full source code for the tutorial is available.
Playing short audio clips in Flutter with Just Audio
Article updated: October 20, 2021
There are a million variables when talking about playing audio in a Flutter app, so this tutorial will seek to limit the scope to one basic task: playing short audio clips. Short audio clips don’t require a background service, setting a playlist, seeking to some position, buffering, or adjusting playback speed. That makes this an ideal topic for your first audio tutorial in Flutter.
There are a number of audio packages on Pub, but this tutorial will be using Just Audio. It supports Android, iOS, web, macOS, Windows, and Linux.
Short audio clips are used in many types of apps. Here are a few examples:
Game sounds
Button click sounds
Language learning app
Dictionary with recorded audio
These sounds are all likely to be just a few seconds long at the most. You can also use Just Audio to play longer media items like songs and podcasts, but this tutorial won’t cover that today.
Follow along step by step to get the most out of this lesson.
Setup
Creating the UI layout
Create a new Flutter project and replace main.dart with the following layout:
Run that and you should see the following:
The goal is to play a cow sound when you press the Cow button and a horse sound when you press the Horse button. If you press them now, though, they won’t do anything. That’s because the onPressed callbacks are currently empty.
First you wait for the player to set the correct audio asset before you play it. The wait isn’t noticeable, though, for short audio clips in the assets bundle.
Restart the app and try it out.
It works!
You’ll notice if you quickly press both buttons in succession, though, that the player cuts off the current clip and starts playing the new one.
You’ll solve that problem next.
Playing different audio clips simultaneously
You can play more than one audio source at a time by creating more than one AudioPlayer instance.
Add the following declaration at the top of _MyAppState:
late AudioPlayer player2;
And in initState add this:
player2= AudioPlayer();
And this in dispose:
player2.dispose();
Now change the Horse button’sonPressed callback so that it uses player2:
Both audio files overlap each other without clipping.
Playing audio clips from the web
If you have a web app or if there are a large number of sounds that you want to use, you might want to play the audio clips by providing a URL.
In order to do that, just replace setAsset with setUrl:
Cow button
final url = 'https://www.applesaucekids.com/sound%20effects/moo.mp3';
await player.setUrl(url);
Horse button
final url = 'https://www.applesaucekids.com/sound%20effects/horse_whinney_2.mp3';
await player2.setUrl(url);
Run the app again and try it out.
It still works, though there is a slight delay while the file loads. If this is an issue you might want to give some visual feedback like a spinning circle. I’ll leave that for another tutorial, though.
Additional notes
If you use HTTP URLs instead of HTTPS ones, then you’ll need to permit that for Android, iOS, and macOS apps. See this answer and the documentation for how to do that.
There are a few other platform-related notes, so check out the documentation to make sure you follow them.
Conclusion
This tutorial was a simple introduction to using Just Audio. There is a lot more that you can do with it, though. Check out the next tutorial in the series for more: