Flutter mobile, playing midi files

I would like to know how can I play midi files on mobile app

Ive found two midi libraries, but don't see how to implement function to play midi files

https://pub.dev/packages/flutter_midi here Im using this package in my app to play sf2 audio, though I can only play single notes at the time, here is code I use to do that

    class KeyPage extends StatefulWidget {
  @override
  _KeyPageState createState() => _KeyPageState();
}

class _KeyPageState extends State<KeyPage> {
  //final _flutterMidi = FlutterMidi(); //TODO 1 check TODO 2

  @override
  void initState() {
    if (!kIsWeb) {
      load(_value);
    } else {
      FlutterMidi.prepare(
          sf2:
              null); //TODO 2 check what to do here with static issue Original line was:
      // _flutterMidi.prepare(sf2: null);
    }
    super.initState();
  }

  void load(String asset) async {
    print("Loading File...");
    FlutterMidi.unmute();
    ByteData _byte = await rootBundle.load(asset);
    // 'assets/sf2/SmallTimGM6mb.sf2';
    // 'assets/sf2/Piano1.sf2';
    FlutterMidi.prepare(sf2: _byte, name: _value.replaceAll("assets/sf2/", ""));
  }

https://pub.dev/packages/dart_midi didn't yet try this package, but from reading it I don't see function in it to play midi file, though language they use is new to me, so I guess this library does that, but I don't understand how

Thank


Here's my implementation using Flutter_midi.

I've used a timer to play the whole sequence of notes, one after the another. For it to work you need to provide the number of notes you want to play and the time between them.

This does work on the IOS emulator, but I want to say that Flutter_midi contains some deprecated code and it doesn't provide the methods to play a full song in midi format.

For example it is not possible to set the velocity of the notes.

I have implemented this solution anyway, but I personally do not recommend this package.

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late FlutterMidi fm;
  final songFile = 'assets/Piano.sf2';

  @override
  void initState() {
    super.initState();

    fm = FlutterMidi();
    _load(songFile);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('MidiPlayer'),
      ),
      body: Center(
        child: TextButton(
          onPressed: () {
            _playSong(
              timing: 100,
              notes: 30,
            );
          },
          child: const Text('Play'),
        ),
      ),
    );
  }

  void _load(String asset) async {
    fm.unmute();
    ByteData _byte = await rootBundle.load(asset);
    fm.prepare(sf2: _byte, name: songFile.replaceAll('assets/', ''));
  }

  void _playSong({
    required int timing, //Time between notes
    required int notes, //Number of notes to be played
  }) {
    var note = 0;
    Timer.periodic(Duration(milliseconds: timing), (timer) {
      //You played the last note
      if (note == notes) {
        fm.stopMidiNote(midi: note - 1);
        timer.cancel();
        return;
      }

      //Interrupt the previous note
      if (note > 0) {
        fm.stopMidiNote(midi: note - 1);
      }

      //Play the current note and increase the counter
      fm.playMidiNote(midi: note++);
    });
  }
}