13.15. Fading Out a Sound

Problem

You want to fade out a sound over a span of a number of milliseconds.

Solution

Create and invoke a fadeOut( ) method.

Discussion

As with fading in a sound, there is no built-in functionality for fading out a sound. However, you can create a custom fadeOut( ) method to handle this task. Our custom fadeOut( ) method is slightly longer than the fadeIn( ) method, but it utilizes similar logic. In addition, the fadeOut( ) method allows you to use two callback functions: one that is called when the sound begins to fade out, and another that is called when the fade out has completed. This can be useful in situations in which you want a particular action or actions to occur when the fade out begins and/or ends. For example, you may want to begin the playback of a second sound when the first begins to fade out. As the second sound fades in, you achieve a cross-fade effect.

As with the fadeIn( ) method, the fadeOut( ) method works only with sounds that are created using our custom createNewSound( ) method.

The fadeOut( ) method accepts up to five parameters:

millis

The number of milliseconds over which the sound should fade out.

minVol

The volume percentage to which the sound should fade (default is 0).

maxVol

The volume percentage from which the sound should start fading (default is current volume).

startTime

The point (in milliseconds) in the sound that the fade out should begin. If no value is specified, the fade out begins millis milliseconds before the end ...

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.