How to Toggle Sound Globally (AS2)

ActionScript 2.0 Flash

Flash is a great medium for presenting sound, whether narration, music, or basic UI clicks and whirs.  That said, it’s a good idea to provide a way for people to toggle sound off, if they prefer silence.  You may have heard or read about the global stopAllSounds() function.  It’s good in a pinch, because it does exactly what its name implies:  any sound currently playing is abruptly shut off.  Of course, additional audio in subsequent frames continues to be heard.  If other buttons are wired to produce sound, they continue to do so when clicked.  stopAllSounds() isn’t a toggle, it’s just an immediate silencer.

This topic comes up a lot in the forums, actually.  You may have heard about using the Sound class to trigger sounds with ActionScript.  It’s possible in this way to check against a single, globally accessible Boolean variable before invoking Sound.start().  Naturally, this means that every last sound in your SWF would have to be programmed, which may be a daunting prospect.  Personally, I’m a fan of the Sound class.  It’s great for games and ActionScript-based presentations, but there’s certainly something to be said for the drag-and-drop utility of placing sound assets by hand in a timeline.  So … is there an easy way to toggle these sounds (any sounds) off — and then on again?  You betcha. 

An answer, short and sweet

Add the following to frame 1 of your scripts layer.

var globalVolume:Sound = new Sound();

I know, pretty short, right?  This allows you to adjust the volume of the whole SWF from two buttons.  Assuming the instance names btnSoundOff and btnSoundOn

btnSoundOff.onRelease = function():Void {
 globalVolume.setVolume(0);
}
btnSoundOn.onRelease = function():Void {
 globalVolume.setVolume(100);
}

How it works

A variable is declared with the arbitrary name globalVolume.  This points to an instance of the Sound class, so the declaration includes the :Sound suffix.  The Sound instance is generated by the constructor new Sound(), which is built to accept an optional parameter (a reference to a movie clip) — but since it’s optional, we’re leaving it out.  Why?  When the parameter is omitted, the Sound instance governs audio across the whole SWF.

The buttons simply invoke the Sound.setVolume() method on our globalVolume instance.  Enter 0 to silence the SWF and 100 to unsilence it.

Variation

Two buttons are fine, but often enough, you see a single button that handles the whole procedure.  Let’s see how to accomplish that.  All we need is a Boolean to keep track of what state we’re in:  volume on or off.

var volumeOn:Boolean = true;
var globalVolume:Sound = new Sound();
btnSoundOff.onRelease = function():Void {
 if (volumeOn) {
  globalVolume.setVolume(0);
 } else {
  globalVolume.setVolume(100);
 }
 volumeOn = !volumeOn;
}

The if statement could be written like this …

if (volumeOn == true) { … };

… but the shorter version makes just as much sense:  all an if statement does is evaluate an expression to decide if the result is true or false — which are precisely the two values volumeOn returns.

Note that if volumeOn returns true, globalVolume.setVolume() is set to 0 and vice versa, but in all cases, the value of volumeOn is set to its own opposite.  The logical NOT operator (!) handles that part (volumeOn equals not-volumeOn; in other words, whatever volumeOn isn’t).

Leave a Reply