getDuration method

Future<Duration?> getDuration()

Returns the duration of the audio.

If the duration is requested for the first time it will use the pool to get or create a player and get the duration from it. Subsequent calls will return the duration immediately.

Implementation

Future<Duration?> getDuration() async {
  if (duration != null) {
    return duration;
  }

  return _lock.synchronized(() async {
    final player = await _createNewOrReserveAnAvailablePlayer();
    duration = await player.getDuration();

    if (availablePlayers.length >= maxPlayers) {
      await player.dispose();
    } else {
      availablePlayers.add(player);
    }

    return duration;
  });
}