How to Play a Custom Sound In Flutter

How to Play a Custom Sound In Flutter?

Earlier we have been through Step By Step Guide to Implement Firebase with Flutter. when we use firebase to store data from the application so in this article we will go through how to Play a Custom Sound in Flutter.

In this blog post, we’ll take you through the process of playing custom sounds in your Flutter applications. Whether you want to add unique audio effects, notifications, or game sounds, this comprehensive guide will equip you with the knowledge and techniques to integrate custom sounds seamlessly. We’ll explore different approaches to playing custom sounds, including using Flutter’s built-in audio packages, integrating audio files, and managing sound playback. You’ll learn how to incorporate sound assets, control volume and playback duration, and handle sound-related events.

How to Play a Custom Sound In Flutter ??

From the plugin’s README:

Future play() async {
  final result = await;
  if (result == 1) setState(() => playerState = PlayerState.playing);

// add a isLocal parameter to play a local file
Future playLocal() async {
  final result = await;
  if (result == 1) setState(() => playerState = PlayerState.playing);

Future pause() async {
  final result = await audioPlayer.pause();
  if (result == 1) setState(() => playerState = PlayerState.paused);

Future stop() async {
  final result = await audioPlayer.stop();
  if (result == 1) {
    setState(() {
      playerState = PlayerState.stopped;
      position = new Duration();

A simple solution for playing a file already defined in assets is using AudioCache. Library: More about AudioCache After adding the library to pubspec.yaml, import required class:

add an asset in the same file and place the file with sound to the assets folder (if you don’t have this folder, create it)

- assets/sound_alarm.mp3

then add this code:

static AudioCache player = new AudioCache();
const alarmAudioPath = "sound_alarm.mp3";;

An example here

The audio players’ works

  • Add the library to your pubspec.yaml:
    audioplayers: ^5.2.1
  • In pubspec.yaml under flutter add the reference to your assets file:
           - assets/yes.mp3

    MAKE SURE it is under the assets folder. It does not work when it is in a subfolder. For example, something like – assets/sounds/yes.mp3 will not work. Just put your audio file in the assets folder, not in its subfolder.

  • import the library in your app as an import package:audioplayers/audioplayers.dart;
  • then define this function:
    Future<AudioPlayer> playLocalAsset() async {
        AudioCache cache = new AudioCache();
       //At the next line, DO NOT pass the entire reference such as assets/yes.mp3. This will not work.
       //Just pass the file name only.
        return await"yes.mp3"); 
  • call the function whenever you need to play a sound: await playLocalAsset();


Hope you enjoyed this article !!!

In this article, we have been through how to play sound in flutter.

Do not forget to drop your valuable suggestions/feedback. We would love to assist you. is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

Abhishek Dhanani

Written by Abhishek Dhanani

Abhishek Dhanani, a skilled software developer with 3+ years of experience, masters Dart, JavaScript, TypeScript, and frameworks like Flutter and NodeJS. Proficient in MySQL, Firebase, and cloud platforms AWS and GCP, he delivers innovative digital solutions.

Leave a comment

Your email address will not be published. Required fields are marked *

Discuss Your Project

Connect with Flutter Agency's proficient skilled team for your app development projects across different technologies. We'd love to hear from you! Fill out the form below to discuss your project.

Build Your Agile Team

Hire Skilled Developer From Us

"*" indicates required fields

✓ Valid number ✕ Invalid number

ready to get started?

Fill out the form below and we will be in touch soon!

"*" indicates required fields

✓ Valid number ✕ Invalid number