Playing small sounds in Flutter

Md Sadab Wasim
Aesthetic coders
Published in
1 min readApr 27, 2022

play sounds without any heavy libraries

Photo by Icons8 Team on Unsplash

Oftentimes you want to play small sounds in your app when you are showing some dialog, snack bar, or notification and today I’ll tell you how to play small sounds efficiently.

install this lightweight plugin:

soundpool: <latest version>

once you have this plugin installed let’s make a sound service class.

import ‘package:flutter/services.dart’;
import ‘package:soundpool/soundpool.dart’;
class SoundService {
Soundpool? pool;
int? soundId;
SoundService() {
init();
}
Future init() async {
pool = Soundpool.fromOptions();
soundId = await rootBundle
.load(“assets/sound/success_sound.mp3”) //your sound file name here
.then((ByteData soundData) {
return pool!.load(soundData);
});
}
playSound() {
pool!.play(soundId!);
}
}

After writing this service initialize this service only once in your app, and whenever you want to play the sound just call the playSound method of this service.

In this way, we don’t have to use heavy libraries like audio_player to play a simple small sound and this implementation will make our app structure cleaner and more efficient.

Thanks for reading,

please applaud if you find this article useful.

--

--