play method

Future<void> play()

Toca o exemplo de agora. O motor de som abre só na primeira vez.

Implementation

Future<void> play() async {
  final generation = _generation;
  _playTimer?.cancel();
  final engine = _engine ??= await deps.ensureEngine();
  if (isClosed || generation != _generation) return;
  if (engine == null) {
    emit(state.copyWith(noSound: true));
    return;
  }
  if (!_engineStarted) {
    _engineStarted = true;
    try {
      await engine.start();
    } on Object {
      // Já aberto: segue.
    }
    if (isClosed || generation != _generation) return;
  }
  final seconds = playTimed(engine, state.preview.notes);
  emit(state.copyWith(noSound: false, playing: true));
  _playTimer = deps.clock.timer(
    Duration(milliseconds: (seconds * 1000).round()),
    () {
      if (!isClosed) emit(state.copyWith(playing: false));
    },
  );
}