Sound.loadSound( ) Method | Flash 6 |
download an external sound file into the Player |
A string specifying the network or local path of the sound file to load.
A Boolean specifying the type of sound to load: either streaming (true) or event (false).
The loadSound( ) method loads an external sound file from url. In Flash 6, only MP3 files can be loaded via loadSound( ). If isStreaming is true, the loaded sound will behave as a streaming sound and will automatically begin playback as soon as the required buffer has loaded (see the global _soundbuftime property). If isStreaming is false, the loaded sound will behave as an event sound and will not begin playing automatically. To play a loaded event sound, use Sound.start( ) after the sound has fully loaded (see the following Example). To stop the playback of a loaded sound (either streaming or event), use Sound.stop( ). In versions of the Flash Player prior to 6.0.40.0, a streaming sound cannot be played via Sound.start( ). See that method's entry for more information.
A loading sound file's download progress can be monitored via getBytesLoaded( ) (see that method's entry for sample preloading code). When the sound finishes loading, Sound.onLoad( ) executes. If the sound file cannot be found at url, loadSound( ) fails silently and Sound.onLoad( ) executes with a success parameter set to false.
To load a sound from a movie's Library instead of from a file, use attachSound( ). Unlike attached sounds, multiple loaded sounds can be loaded into a single Sound object simultaneously. However, the position and duration properties and the start( ) method apply only to the most recently loaded sound. Hence, for best control over multiple sounds, each sound file should be loaded into its own Sound instance. When both attached and loaded sounds are present in a single Sound object, start( ) applies to the loaded sound, even if the attached sound is added after the loaded sound is loaded. For example:
// Load track1.mp3 bgMusic.loadSound("track1.mp3"); // Sometime after track1.mp3 loads, attach track2 using attachSound() bgMusic.attachSound("track2"); // Play the sound... bgMusic.start(); // Plays track1.mp3 (the loaded sound)
Flash can play a maximum of eight individual sounds simultaneously. When loaded via attachSound( ), each sound needs its own Sound instance.
To determine whether the Flash Player being used supports MP3 audio playback, use System.capabilities.hasMP3.
Compressed MP3 files often contain minor imperfections (such as an initial delay) that affect looped playback in Flash. To compensate, a loaded event sound can be offset by a small amount, as follows:
loopSound.start(0.05, 3);
However, this technique does not account for any ending silence on the sound, so results may vary. For best loop performance, use ADPCM or MP3 compression directly in Flash and attach the loop sound from the Library using attachSound( ).
To load a sound, we first create a Sound object, then call the loadSound( ) method. Often, it's also a good idea to respond to the completion of the sound's download by defining a callback function for onLoad( ), as shown in the following example. First, we load an event sound; notice that the onLoad( ) handler must be defined before the loadSound( ) call:
// Create a Sound object music = new Sound(); // Define an onLoad() callback function for our object music.onLoad = function () { // Place code here to respond to sound load completion trace("Sound has fully loaded"); // It's an event sound and it's fully loaded, so we can safely play it now this.start(); } // Load an event sound music.loadSound("http://www.somesite.com/song.mp3", false);
Now, we load the same sound, but as a streaming sound. It's not as important to define an onLoad( ) callback for a streaming sound because the sound starts playing automatically when it has been buffered sufficiently:
// Create a Sound object music = new Sound(); // Load a stream sound music.loadSound("http://www.somesite.com/song.mp3", true);
capabilities.hasAudio, Sound.getBytesLoaded( ), Sound.getBytesTotal( ), Sound.onLoad( ), _soundbuftime