List<String> getOptimalAudioUrls(String primaryUrl)

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

Source

List<String> getOptimalAudioUrls(String primaryUrl) {

  var availableTypes = AudioLoader.supportedTypes.toList();
  if (!this.mp3) availableTypes.remove("mp3");
  if (!this.mp4) availableTypes.remove("mp4");
  if (!this.ogg) availableTypes.remove("ogg");
  if (!this.opus) availableTypes.remove("opus");
  if (!this.ac3) availableTypes.remove("ac3");
  if (!this.wav) availableTypes.remove("wav");

  var urls = new List<String>();
  var regex = new RegExp(r"([A-Za-z0-9]+)$", multiLine:false, caseSensitive:true);
  var primaryMatch = regex.firstMatch(primaryUrl);
  if (primaryMatch == null) return urls;
  if (availableTypes.remove(primaryMatch.group(1))) urls.add(primaryUrl);

  if (this.alternativeUrls != null) {
    for(var alternativeUrl in this.alternativeUrls) {
      var 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;
}