flutter_midi
flutter_midi copied to clipboard
Midi note play/stop note not working if inside of Future.Delayed
I can't seem to schedule notes on/off using Future.delayed. Am I doing something wrong?
Futureload(String asset, FlutterMidi midiPlayer) async { midiPlayer.unmute(); // Optionally Unmute ByteData _byte = await rootBundle.load(asset); await midiPlayer.prepare(sf2: _byte); } void playNotes(String asset) async { FlutterMidi midiPlayer = FlutterMidi(); await load(asset, midiPlayer).then((value) { midiPlayer.playMidiNote(midi: 60); Future.delayed(Duration(seconds: 1), () { print ('stops now'); // this prints, but the note continues playing as if there were no noteOff midiPlayer.stopMidiNote(midi: 60); }); // if we comment out the previous Future.delayed and uncomment the next two lines, // the noteOff comes at the correct time (but of course sleep blocks the main isolate, // breaking the UI; so we can't use this method) // sleep(Duration(seconds: 1)); // midiPlayer.stopMidiNote(midi: 60); }); }
I have tried spawning a new isolate so I could use sleep, but there were many error logs and then neither notes on nor notes off would work at all.
This appears to be caused by the fact I am using this inside the then
of load? Perhaps, something is still not initialized correctly by the time that future resolves? If I move the note on and note off into a separate method which is called upon a button click, both the note on and the note off work correctly.