Sound.duration Property

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
Sound.duration Property Flash 6

the total length of the sound, in milliseconds read-only
soundObject.duration

Description

The duration property returns the length, in milliseconds, of the attached or loaded sound associated with soundObject. It does not apply to timeline sounds placed at authoring time. If multiple sounds are loaded into a single soundObject, the duration property reflects the most recently loaded sound. If the sound is looped, duration returns the length of one loop. If no attached or loaded sound exists, duration returns undefined. While an external streaming sound is loading, duration reflects the length of the sound loaded so far. While an external event sound is loading, duration stores 0 until the sound has completely loaded, at which point it stores the actual length of the sound. Theoretically, duration should be available from Sound.onLoad( ), but a bug in Flash 6 prevents access to it (see the following Bugs).

The duration property can be used with position to determine the percentage of a sound that has played so far. See Sound.position for example code that increases the volume of a sound proportionate to the percentage played.

Bugs

In Flash 6, when a sound is exported from the Library and loadSound( ) is invoked on soundObject, the duration property matches the Library sound's length until part of the external sound has been downloaded to the Player. To ensure an accurate reading for duration, test its value only after getBytesLoaded( ) returns a positive value.

In Flash 6, the duration property is not initially available within onLoad( ). Use setInterval( ) to poll for a valid duration, as follows:

song = new Sound(); song.onLoad = function (success) {   // Because of the bug, no duration displays   trace("Initial Duration: " + this.duration);       // Store a reference to the current sound   var theSound = this;       // Create a function that checks if duration exists   function pollForDuration () {     if (theSound.duration >= 0) {       // Show the duration, now that it's available       trace("Actual duration: " + theSound.duration);       // Play the sound       theSound.start();       // Stop polling for a valid duration       clearInterval(intID);     }   }       // If the sound loaded properly...   if (success =  = true) {     // Repeatedly check for a valid duration     var intID = setInterval(pollForDuration, 100);   } }     // Load the MP3 file song.loadSound("track1.mp3", false);

Example

The following code attaches the exported sound "JingleBells" to a Sound object and then checks the length of the sound:

music = new Sound(); music.attachSound("JingleBells"); trace(music.duration);  // Displays length of JingleBells, in milliseconds

See Also

Sound.position



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net