Thursday, March 28, 2024

FLutter Plugin to Play midi on iOS and Android

@flutter_midi is a open sourced flutter Plugin to Play midi on iOS and Android. This package accept .sf2 files as input.

For online demonstration please visit : https://rodydavis.github.io/flutter_midi/

Installation

Add following to your package’s pubspec.yaml file:
Check following url to get latest version on Pub and replace below section

dependencies:
flutter_midi_platform_interface: ^0.0.1

After save the flutter_midi packages are automatically downloaded and included into your project.

How to use

  1. Download a any sound font file, example: sound_font.SF2 file.
  2. Create an /assets folder and store the .sf2 files
  3. Update assets path in pubspec.yaml
assets:
   - assets/sf2/Piano.SF2
   - assets/sf2/SmallTimGM6mb.sf2

Load the sound font to prepare to play;

 @override
  void initState() {
    load('assets/sf2/Piano.SF2');
    super.initState();
  }
  
 void load(String asset) async {
    FlutterMidi.unmute(); // Optionally Unmute
    ByteData _byte = await rootBundle.load(asset);
    FlutterMidi.prepare(sf2: _byte);
  }

Play and Stop the Midi Notes

 FlutterMidi.playMidiNote(midi: 60);

 FlutterMidi.stopMidiNote(midi: 60);