How to Play a Custom Sound In Flutter??

· 3 min read
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…

How to Play a Custom Sound In Flutter ??

From the plugin’s README:

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

// add a isLocal parameter to play a local file
Future playLocal() async {
  final result = await audioPlayer.play(kUrl);
  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: https://pub.dartlang.org/packages/audioplayers. 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:
- assets/sound_alarm.mp3

then add this code:

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

An example here

The audio players’ works

  • Add the library to your pubspec.yaml:
    audioplayers: ^0.15.1
  • In pubspec.yaml under flutter add the reference to your assets file:
    flutter
        assets:
           - 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 cache.play("yes.mp3"); 
    }
  • call the function whenever you need to play a sound: await playLocalAsset();

Conclusion:

Hope you enjoyed this article !!!

In this article, we have been through how to play a custom sound in flutter??

Do not forget to drop your valuable suggestions/feedback. We would love to assist you.

FlutterAgency.com 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.

FlutterAgency.com 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.

Leave a Reply