O'Reilly logo

Actionscript Cookbook by Joey Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

13.14. Fading In a Sound

Problem

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

Solution

Create and invoke a custom fadeIn( ) method.

Discussion

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:

millis

The number of milliseconds over which the sound should fade in

minVol

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

maxVol

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

startFadeTime

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

startSound

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

startPlayOffset

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required