views:

427

answers:

4

I've seen many web pages with a simple sound on /sound off button which plays music or some mp3 file when you press sound on and turns it off when you press off.

How do I do that?

Hi, I wasn't planning on using Flash -- if there is a standard plugin I could use and then make modification to the script, that'd be cool.

I'm open to whatever is the "standard" way to do it.

A: 

Are you using Flash? In that case you can do it like this:

stopAllSounds();

or

theSound.stop(["id"]);
miccet
A: 

Assuming you are using Flash as above.

AS2

stopAllSounds();

AS3

var xf:SoundTransform = SoundMixer.soundTransform;
xf.volume = 0;
SoundMixer.soundTransform = xf;
Typeoneerror
+2  A: 

Here's the proper way to do it in AS3.

Initialization:

var sound:Sound;
var channel:SoundChannel;
var pos:Number;
var numLoops:Number = 0; // 0 to loop forever

sound = new Sound();
sound.load( new URLRequest("song.mp3") );
channel = sound.play( 0, numLoops );

Stop playback:

pos = channel.position;
channel.stop();

Start playback:

channel = sound.play( pos, numLoops );

It's true that you could toggle the volume to zero and back, but this leaves needless overhead, and when you restart the sound it will have advanced from where you "stopped" it.

fenomas
"this leaves needless overhead"Very true. The question was phrased "turns it off", not "stops playing"; I incorrectly assumed volume.
Typeoneerror
A: 

I used google and then I used pixel1 standalone

Angela