actionscript sound: still hear volume set to negative value?

A negative volume is still audible? Wait, that can’t be.

Just got bit by this one today, so a little reminder for myself here. Using ActionScript 2 I was creating a little slider widget for controlling volume. Easy peasy, right? Get the position of a slider relative to a slider bar to calculate the volume. Then use setVolume on a Sound object to, well, set the volume.

So far so good. The catch came with those calculations, which are based on graphics with x positions that are not necessarily straight forward (talk to the graphics department, I’m on coffee break!). Sometimes the value I was getting came out negative… but the sound was still audible! Hmmm. Strange.

Well, not really. The docs tell you that the volume can range from 0 to 100. Zero. Not -1 or something. But you can “set” the volume to -1 with mySound.setVolume( -1 ). Yet you might still hear something…

Why’s that? It seems that if you do that (silly you!), the Sound object will play at the last volume it was set to. In this case that was a quiet 2 or so, depending on how fast the slider had been moved down. So if you’re setting the volume based on wacky calculations that come up with a value less than zero, you have to set that explicitly to zero before you set the sound’s volume. Easy peasy.

Why doesn’t it just work that way anyway?

This entry was posted in flash, actionscript, flex and tagged , . Bookmark the permalink.