I’m using Flutter, a cross-platform app development platform, to develop my app. One advantage of Flutter is that it has a large ecosystem of plugin packages that make it possible to add features, such as audio, with a few lines of code. Here’s an example:
import 'package:soundpool/soundpool.dart';
Soundpool pool = Soundpool(streamType: StreamType.notification);
int soundId = await rootBundle.load("sounds/dices.m4a").
then((ByteData soundData) {
return pool.load(soundData);
});
int streamId = await pool.play(soundId);
I tried a number of Flutter audio packages with varying amounts of latency:
- flutter_ogg_piano (Android only)
- audio_players
- soundpool
So it seems that while Flutter packages are easy to use, there isn’t one available that gives low playback latency on iOS. This led me to investigate some alternatives:
- Writing native code: this negates Flutter’s cross-platform advantage and would require a separate codebase for iOS and Android. With a larger team and budget, this may be the way to go.
- Using a 3rd party library. I investigated a couple of libraries:
– Superpowered: -no Flutter support, -high annual cost
– BASS: -no Flutter support, +one-time license fee scales with commercial use case, +well supported
It seemed that BASS is the preferred choice, so I investigated how to integrate an external library using Flutter’s FFI (foreign function interface) capabilities. BASS is provided with precompiled libraries for iOS and Android as well as a header file defining the API. I used a Flutter package called ffigen to generate Dart code for calls to functions in the library.
I made a Flutter package to help others use BASS in Flutter. See flutter_bass. It includes an example app that has a comparison of BASS to soundpool and flutter_ogg_piano. Here are the results of measuring the button-press-to-sound-play latency with the example app on both iOS and Android devices comparing BASS to soundpool and flutter_ogg_piano. Note that these latencies include the time for Flutter’s gesture recognizer to sense the button press.