US Office

1176 Shadeville Rd, Crawfordville Florida 32327, USA

 +1 (850) 780-1313

India Office

Office No 405, Kabir Shilp, Opp. Kansar Hotel, Opp. Landmark, Kudasan, Gandhinagar, Gujarat 382421

[email protected]

How to Play Custom Sound In Flutter?

How to Play Custom Sound In Flutter

How to Play Custom Sound In Flutter?

Sometimes while building application like alarm manager user need to notify frequently about the current timings and sounds needs to be played. Earlier we went through articles related to this here. So in this article, we will go through how to play custom sound in Flutter.

How to Play Custom Sound In Flutter?

To play custom sound in flutter we can use this plugin adds audio support to Flutter:

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 'package:audioplayers/audio_cache.dart';

add an asset in the same file and place the file with sound in 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";;

Check out an example here

Add the library to your pubspec.yaml and

audioplayers: ^0.15.1

In pubspec. yaml under flutter add the reference to your assets file:

       - assets/yes.mp3

import the library in your app as

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();


Thanks for Reading !!! Stay Connected ūüôā

So In this article, we have been through how to play custom sound in Flutter.

Keep Learning !!! Keep Fluttering !!! Lots of amazing content coming up your way.

Drop your valuable suggestions/feedback in the comments right below!!

Still, need Support for Flutter Development? Do let us know, we would love to help you.

Flutter Agency is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget Guide, Flutter Projects, Code libs and etc.

Flutter Agency is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.

  • August 2, 2021

    Very good blog, especially for people who are working in a flutter app development company in India. Cheers.

Post a Comment