Future<Sound> load(String url, [ SoundLoadOptions soundLoadOptions ])

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();

Source

static Future<Sound> load(String url, [SoundLoadOptions soundLoadOptions]) async {

  var options = soundLoadOptions ?? Sound.defaultLoadOptions;
  var audioUrls = options.getOptimalAudioUrls(url);
  var audioContext = WebAudioApiMixer.audioContext;
  var aggregateError = new AggregateError("Error loading sound.");

  for(var audioUrl in audioUrls) {
    try {
      var httpRequest = await HttpRequest.request(audioUrl, responseType: 'arraybuffer');
      var audioData = httpRequest.response as ByteBuffer;
      var audioBuffer = await audioContext.decodeAudioData(audioData);
      return new WebAudioApiSound._(audioBuffer);
    } catch (e) {
      var loadError = new LoadError("Failed to load $audioUrl", e);
      aggregateError.errors.add(loadError);
    }
  }

  if (options.ignoreErrors) {
    return MockSound.load(url, options);
  } else {
    throw aggregateError;
  }
}