Problem Solving: Mute/Unmute

I wanted to create a button that could mute the sound and then unmute it again. From what I had learnt in class I couldn't do this. The best solution we had been taught was to stop all sounds, but if you are presenting a complex series of events with sounds, stopping and starting them isn't going to keep everything in sync. I also wanted to have it all on 1 button to keep the interface tidy, but couldn't work this out. After looking at various tutorials and resources I managed to piece together different functions in to what I wanted to do, and had a lot of trouble getting it to work along the way.

- - - - - - - - - - - - - - - -

My first problem was getting the sounds muted. After poruing through tons of actionscript, I managed to be able to set custom volumes for sounds following certain actions (such as on press) but it would only work for files specifically loaded with that function attached to it. I eventually found a brilliant technique where you create a global sound file, which when muted mutes all other sounds. The referenced sound itself doesn't exist but it allows you to manipulate all volume.

_root.globalSound = new Sound();

Here is the code I wrote:

//Audio Off Button
muted = false;
audio_off.onRelease = function(){
if(muted == false){
_root.globalSound.setVolume(0);
muted = true;
}
}
//End Audio Off Button

It basically says to that it's not muted at the start, and on button press (if not muted) change the volume of the global sound file to 0 effectively muting all sound and then sets muted to true. Then on the unmute button I wrote this code:

//Audio On Button
audio_on.onRelease = function(){
if(muted == true){
_root.globalSound.setVolume(100);
muted = false;
}
}
//End Audio On Button

This part says that on press of the unmute button, (if muted is true which it should be because it's set true at the end of the press of the mute button) then to turn the volume of the global file to 100 and to make muted false to reset everything back to the way it was before.

Problem solved.

- - - - - - - - - - - - - - - -

My second problem was that I wanted it to all be done on one button. While I didn't achieve this, it's now irrelevant as I did it with 2 buttons but the illusion of just one. My solution was to have 2 layers. On top would be a layer with the mute button, and underneath that would be the unmute button.

In the end I had this code:

//Audio On Button
audio_on.onRelease = function(){
if(muted == true){
_root.globalSound.setVolume(100);
audio_off._visible = true;
muted = false;
}
}
//End Audio On Button


//Audio Off Button
muted = false;
audio_off.onRelease = function(){
if(muted == false){
_root.globalSound.setVolume(0);
audio_off._visible = false;
muted = true;
}
}
//End Audio Off Button

It basically syncs the visibility of each button with the audio's mute status. If mute = false then the mute button shows, but if mute = true the mute button becomes invisible revealing the unmute button and pressing that changed the status back to mute = false so the mute buttons shows again.

Problem solved.