To play an audio, it's really simple! Just run, at any moment:
import 'package:flame/flame.dart'; Flame.audio.play('explosion.mp3');
Or, if you prefer:
import 'package:flame/audio.dart'; Audio audio = new Audio(); audio.play('explosion.mp3');
The difference is that each instance shares a different cache. Normally you would want to use the
Flame.audio instance and totally share the cache.
You must have an appropriate folder structure and add the files to the
pubspec.yaml file, as explained above.
It can be an MP3, OGG or a WAV file (those are the ones I tested and it worked)
This uses the audioplayers lib, in order to allow playing multiple sounds simultaneously (crucial in a game). You can check the link for more in-depth explanations.
If you want to play indefinitely, just use
Finally, you can pre-load your audios. Audios need to be stored in the memory the first time they are requested; therefore, the first time you play each mp3 you might get a delay. In order to pre-load your audios, just use:
You can load all your audios in beginning so that they always play smoothly; to load multiple audios, use the
Finally, you can use the
clear method to remove something from the cache:
There is also a
clearAll method, that clears the whole cache.
This might be useful if, for instance, your game has multiple levels and each has a different soundtrack.
Both load methods return a
Future for the
loop you can pass an additional optional double parameter, the
volume (defaults to
loop methods return an instance of a
AudioPlayer from the audioplayers lib, that allows you to stop, pause and configure other specifications.
There's lots of logs; that's reminiscent of the original AudioPlayer plugin. Useful while debug, but afterwards you can disable them with: