AS3 Toggle Button with MuteSounds
/* I have an educational slideshow template that has audio, and I needed a button to mute the audio on demand. I also applied a sort of 'grey out' effect via alpha, in case the button has already been clicked. I also used this same code to show or hide the English translation for pages, which made me think that I better save this snippet for future use! By Stacey Tipton Reiman, www.akwebgenius.com/blog */
// your imports - in addition to all of the regular stuff, you need to have SoundTransform for this to work...
import flash.media.SoundTransform;
// you'll need the audioBtn
[Embed(source="C:/path_to_your_btn/audio.swf")]
public var AudioBtn:Class;
var audioBtn = new AudioBtn();
audioBtn.width = 35
audioBtn.height = 35
audioBtn.x = 595
audioBtn.y = 5
addChild(audioBtn)
// and your event listener needs to be added...
audioBtn.addEventListener(MouseEvent.MOUSE_DOWN, muteSounds);
// you also need a textField and a format [format is not included...]
var audio_toggle_txt:TextField = new TextField;
addChild(audio_toggle_txt);
audio_toggle_txt.x=563;
audio_toggle_txt.y=45;
audio_toggle_txt.embedFonts = true;
audio_toggle_txt.wordWrap = true;
audio_toggle_txt.autoSize="left";
audio_toggle_txt.defaultTextFormat = format;
audio_toggle_txt.text = 'sound' + '\n' + 'control' ;
// Variable to detect whether the number of times the toggle button has been clicked.
var clickOnce:uint=0;
function muteSounds(event:MouseEvent):void
{
clickOnce++;
if (clickOnce==1)
{
SoundMixer.soundTransform = new SoundTransform(0);
audioBtn.alpha = .4;
audio_toggle_txt.text = 'audio' + '\n' + 'OFF';
}
if (clickOnce==2)
{
SoundMixer.soundTransform = new SoundTransform(1);
clickOnce=0;
audioBtn.alpha = 1;
audio_toggle_txt.text = 'audio' + '\n' + 'ON';
}
}
via [ActionScript 3] AS3 Toggle Button with MuteSounds – Pastebin.com.
