getOptimalAudioUrls method Null safety

List<String> getOptimalAudioUrls(
  1. String primaryUrl
)

Determine which audio files are the most likely to play smoothly, based on the supported types and formats available.

Implementation

List<String> getOptimalAudioUrls(String primaryUrl) {
  final availableTypes = AudioLoader.supportedTypes.toList();
  if (!mp3) availableTypes.remove('mp3');
  if (!mp4) availableTypes.remove('mp4');
  if (!ogg) availableTypes.remove('ogg');
  if (!opus) availableTypes.remove('opus');
  if (!ac3) availableTypes.remove('ac3');
  if (!wav) availableTypes.remove('wav');

  final urls = <String>[];
  final regex = RegExp(r'([A-Za-z0-9]+)$');
  final primaryMatch = regex.firstMatch(primaryUrl);
  if (primaryMatch == null) return urls;
  if (availableTypes.remove(primaryMatch.group(1))) urls.add(primaryUrl);

  if (alternativeUrls != null) {
    for (var alternativeUrl in alternativeUrls!) {
      final alternativeMatch = regex.firstMatch(alternativeUrl);
      if (alternativeMatch == null) continue;
      if (availableTypes.contains(alternativeMatch.group(1))) {
        urls.add(alternativeUrl);
      }
    }
  } else {
    for (var availableType in availableTypes) {
      urls.add(primaryUrl.replaceAll(regex, availableType));
    }
  }

  return urls;
}