start method
Starts playing the audio, returns a function that can stop the audio. You must dispose the audio player yourself if using PlayerMode.lowLatency.
Implementation
Future<StopFunction> start({double volume = 1.0}) async {
return _lock.synchronized(() async {
final player = await _createNewOrReserveAnAvailablePlayer();
currentPlayers[player.playerId] = player;
await player.setVolume(volume);
await player.resume();
StreamSubscription<void>? subscription;
Future<void> stop() {
return _lock.synchronized(() async {
final removedPlayer = currentPlayers.remove(player.playerId);
if (removedPlayer != null) {
subscription?.cancel();
await removedPlayer.stop();
if (availablePlayers.length >= maxPlayers) {
await removedPlayer.release();
} else {
availablePlayers.add(removedPlayer);
}
}
});
}
if (playerMode != PlayerMode.lowLatency) {
subscription = player.onPlayerComplete.listen((_) => stop());
}
return stop;
});
}