How to Play Custom Sound In Flutter?

3 min read
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: https://pub.dartlang.org/packages/audioplayer

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 '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:
- assets/sound_alarm.mp3

then add this code:

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

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:

flutter
    assets:
       - 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 cache.play("yes.mp3"); 
}
  • call the function whenever you need to play a sound: await playLocalAsset();

Conclusion:

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.

2 comments

Leave a Reply