load method Null safety

Future<Sound> load(
  1. String url,
  2. [SoundLoadOptions? soundLoadOptions]
)
override

Loads a sound from a file url.

The file extension in the url will be replaced according to the browser's capability to playback certain kinds of audio types. For example if the url ends with the 'mp3' extension and the browser does not support mp3 playback, the file extension will be replaced with 'ogg' or 'ac3'. You can customize this behavior by changing the soundLoadOptions.

var sound = await Sound.load("assets/audio/hello.mp3");
sound.play();

Implementation

static Future<Sound> load(String url,
    [SoundLoadOptions? soundLoadOptions]) async {
  try {
    final options = soundLoadOptions ?? Sound.defaultLoadOptions;
    final audioUrls = options.getOptimalAudioUrls(url);
    final corsEnabled = options.corsEnabled;
    const loadData = false; // options.loadData;
    final audioLoader = AudioLoader(audioUrls, loadData, corsEnabled);
    final audioElement = await audioLoader.done;
    return AudioElementSound._(audioElement);
  } catch (e) {
    final options = soundLoadOptions ?? Sound.defaultLoadOptions;
    if (options.ignoreErrors) {
      return MockSound.load(url, options);
    } else {
      rethrow;
    }
  }
}