Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Midi note play/stop note not working if inside of Future.Delayed #31

Open
IsaacWallis opened this issue Apr 27, 2021 · 1 comment
Open

Comments

@IsaacWallis
Copy link

IsaacWallis commented Apr 27, 2021

I can't seem to schedule notes on/off using Future.delayed. Am I doing something wrong?

Future load(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.

@IsaacWallis
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant