speakSentences method
void
speakSentences()
Implementation
void speakSentences() {
if (_timer != null) {
var msg =
'Aborted creation of speaker timer since a timer already exists.';
_logger.warn(msg);
return;
}
_timer = Timer.periodic(const Duration(milliseconds: 250), (timer) async {
if (_stopped) {
if (isSpeaking) {
if (player == null) {
_logger.warn('Speaker is stopped but no player was found to stop');
} else {
_logger.info('Stopping current player');
}
player?.stop();
isSpeaking = false;
}
timer.cancel();
_timer = null;
return;
}
if (isSpeaking) {
return;
}
if (_sentences.isEmpty) {
return;
}
isSpeaking = true;
try {
var (sentence, fileFuture) = _sentences.removeAt(0);
await Future.delayed(maxSentenceDelay, () async {
var file = await fileFuture;
var localPlayer = playerFactory(file);
player = localPlayer;
if (onPlay != null) onPlay!(sentence, file);
var playFuture = localPlayer.play();
var volumeStream = localPlayer.getVolumeIntensity();
if (volumeStream != null) {
await for (var volume in volumeStream) {
_volumeController.add(volume);
}
}
await playFuture;
});
} finally {
player = null;
isSpeaking = false;
}
});
}