13.14. Fading In a Sound


You want to fade in a sound from the beginning of playback over a span of a number of milliseconds.


Create and invoke a custom fadeIn( ) method.


Although there is no built-in method for fading in sounds, you can create a custom method for the Sound class to handle this task. Our custom fadeIn( ) method, shown in the following code, accomplishes this by using an onEnterFrame( ) event handler method to monitor the position of the sound’s playback and to set the volume accordingly. The fadeIn( ) method requires that the Sound object is created using our custom createNewSound( ) method from Recipe 13.1 because it uses the sound holder movie clip.

The fadeIn( ) method accepts up to six parameters:


The number of milliseconds over which the sound should fade in


The volume percentage from which the sound should start to fade (defaults to the current volume or 0 if the current volume is 100)


The volume percentage at which the sound fade should end (defaults to 100)


The number of milliseconds from the beginning of the sound at which to begin the fade (defaults to 0)


A Boolean value that indicates whether to start the playback of the sound automatically


The number of milliseconds from the beginning of the sound at which to begin playback

Add the following custom fadeIn( ) method to an external Sound.as file for easy inclusion in other projects:

Sound.prototype.fadeIn = function (millis, ...

Get Actionscript Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.