Table of Contents

Sound.onLoad( ) Event Handler Flash 6

occurs when an external sound finishes loading
soundObject.onLoad(success)

Arguments

success

A Boolean: true if the load succeeded; otherwise, false. If the sound file was not found in a call to loadSound( ), onLoad( ) is executed with a success parameter of false.

Description

The onLoad( ) event handler executes when a sound file has downloaded completely into soundObject due to an earlier call to loadSound( ). To use onLoad( ), we assign it a callback (i.e., a custom-made function) that performs the action we take when our sound loads properly and/or the action we take when the load attempt fails. For example:

// Create the Sound object
carSound = new Sound();
// Assign a callback
carSound.onLoad = function (success) {
  if (success) {
    // Respond to completed sound load operation here
  } else {
    // Execute load-failure code here
  }
}

If the loaded sound is an event sound, we can use onLoad( ) to start playing it, as follows:

carSound = new Sound();
carSound.onLoad = function (success) {
  if (success) {
    this.start();
  } else {
    displayLoadError();
  }
}
carSound.loadSound("http://www.somesite.com/sounds/driving.mp3", false);

The onLoad( ) callback handler should be defined before the call to loadSound( ) is made. Notice that from the body of the onLoad( ) handler we can refer to our sound object with the this keyword.

Before a sound has fully loaded, we can check its download progress with getBytesLoaded( ).

Bugs

The duration, position, and id3 Sound properties are not initially available from within an onLoad( ) handler. See those property entries for details and workarounds.

See Also

Sound.getBytesLoaded( ), Sound.getBytesTotal( ), Sound.loadSound( ), Sound.onSoundComplete( )


Table of Contents