Notes: 13. Play Sound Effects
Here’s flame_audio’s documentation in case you need help.
Previously, you learned about sound effects.
They are used to emphasize content in video games.
Depending on what you are looking to highlight, you can have multiple sound effects:
- Background Music: For menus in-game, pauses or evoking a sense of movement. Usually in the form of music.
- Ambient Noises: For helping the player feel a sense of immersion. They can be sounds like birds, rain or shouting.
- Feedback Sounds: To provide the user the needed feedback when actions are performed like swinging a sword or shooting a bullet.
Overall, sound effects are a crucial part of game development.
They help shape the overall player experience, evoke emotions, improve storytelling, and contribute to the sense of immersion in the game world.
Demo
Flame also simplifies the needed steps to add sound effects to your game.
Using the package flame_audio, you can easily add audio support for the Flame game engine.
This package has a set of default settings that’ll help you like an audioplayer, cache and background manager. You can use these features to include background music, ambient noises or feedback sounds.
pubspec.yaml
For now, just add flame_audio to the your pubspec.yaml. This package let’s you handle sound effects and you’ll be adding some soon.
flame_audio: ^2.0.5
Remember to run flutter pub get after modifying pubspec.yaml.
MeteormaniaGame
Now, in MeteormaniaGame, you’ll have to load the different sound effects like you do with sprites.
await FlameAudio.audioCache.loadAll([
'sfx/explosion.mp3',
'sfx/laser.mp3',
'sfx/destroy_meteorite.mp3',
]);
Right after loading the sound FXs, let’s start playing some background music for the game. Since it’ll be playing in the background, you’ll want to lower the volume to 0.3.
await FlameAudio.bgm.play('music/background_music.mp3', volume: 0.3);
Now, add your first sound effect. In onTapUp, use FlameAudio to play the laser effect.
FlameAudio.play('sfx/laser.mp3');
Build and run the game. Right from the start you now hear the music of the game. Tap the screen to shoot a couple of laser bullets and listen for the new sound effect you just added.