Sound.position Property

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

the length of time the sound has played, in milliseconds read-only
soundObject.position

Description

The position property indicates how long an attached or loaded sound has been playing, in milliseconds. If multiple sounds are loaded into a single soundObject, the position property reflects the first loaded sound. See the following Bugs for details. The position property can be used to cue animations that synch directly to the playback of a sound. For example, this code plays an explosion animation after a sound has played for five seconds:

// Create the sound introMusic = new Sound(); // Attach the Library sound exported as "song" introMusic.attachSound("song");     // Every frame, check if the sound has played for 5 seconds this.onEnterFrame = function () {   // If the sound has played for 5 seconds...   if (introMusic.position > 5000) {     // Show the explosion     this.gotoAndPlay("explosion_animation");     // Stop checking the sound position     delete this.onEnterFrame;   } }     // Start playing the sound  introMusic.start();

Used with the duration property, position can also tell us what percentage of a sound has played so far (see the following Example). However, for looped sounds, position resets to 0 with each loop. Hence, to determine the percentage of a looping track that has played, the position of each loop must be tallied and compared to the sound's duration multiplied by the number of loops.

Before a sound plays, its position property returns 0, but after a sound completes, position is not reset to 0; instead, it equals the sound's duration.

The position property is read-only and cannot be used to play a sound at a certain position. Use Sound.start( ) with an offset value to start a sound at a specified time offset.

Bugs

In Flash 6, the position property is not reset for each sound loaded into a single Sound instance. For example, if the instance theMusic loads and completely plays song1.mp3 and then loads song2.mp3, its position property will be equal to song1.mp3's total duration. To avoid this problem, load each sound into a unique Sound instance. Where possible, delete completed instances to save memory.

In Flash 6, while a streaming sound is loading, the position property can be inaccurate (on the order of milliseconds). Use nonstreaming sounds when position accuracy is critical (for example, when synching multiple sounds).

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

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

Example

The following code adds to all sounds a startFadeIn( ) method that increases the volume of a sound proportionate to the amount played. As is, the method works with attached sounds only, because it lacks the preloader code required for applying the effect to loaded sounds:

/*  * Method: Sound.startFadeIn()  *   Desc: Fades a sound in from 0-100% volume.  * Params: fadeDuration.  The length, in milliseconds, of the fade in.  */ Sound.prototype.startFadeIn = function (fadeDuration) {   this.setVolume(0);   this.fadeDuration = (fadeDuration =  = undefined) ? this.duration : fadeDuration;   this.volInterval = setInterval(this, "fadeIn", 10); }     /*  * Method: Sound.fadeIn()  *   Desc: Internal method that performs a sound fade. Called by startFadeIn().  */ Sound.prototype.fadeIn = function () {   this.vol = Math.floor(this.position / this.fadeDuration * 100);   this.setVolume(this.vol);   // Stop calling fadeIn() if the sound has finished playing   if (this.fadeDuration < this.position) {     clearInterval(this.volInterval);   }   trace("volume: " + this.vol); }     // Usage: // Create a new Sound object. var music = new Sound();     // Attach a sound from the Library and play it music.attachSound("song"); music.start();     // Invoke the startFadeIn() method with a 2 second fade (2000 milliseconds) music.startFadeIn(2000);

See Also

Sound.attachSound( ), Sound.duration, Sound.loadSound( )



    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