Marianna
Marianna

Reputation: 129

mute unmute button in actionscript 3

i need some help with my actionscript 3 project. I have a button with a sound in it. I have some code (see below) that when i press the button it plays the sound and if i press the button again it stops the sound (like a mute/unmute button). The problem is that when i press the button to play the sound for the second time it plays two sounds (the same sound twice) and if i press the button to play the sound more times the same sound plays many times. Can you please help me solve the problem? Thank you.

   function setMute1(vol){
       sTransform1.volume = vol; 
       SoundMixer.soundTransform = sTransform1;
  }

   var sTransform1:SoundTransform = new SoundTransform(1,0);
   var Mute1:Boolean = true;
   sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);

   function toggleMuteBtn1(event:Event) {
    if(Mute1 === false) {
        Mute1 = true;
        setMute1(0);
     } else {
         Mute1 = false;
         setMute1(1);
     }
  }

Upvotes: 0

Views: 6727

Answers (2)

user2650087
user2650087

Reputation: 76

From what I understand you start the sound by assigning it to the buttons hit frame? You need to have the sound started by the code in order to control the sound in good way.

Here is an working example based on your code, that loads an external mp3 file. The sound is played and stopped via the same button.

// load the sound
var mySound:Sound = new Sound();
mySound.load(new URLRequest("loop.mp3"));
var myChannel:SoundChannel = new SoundChannel();
// tool you need for manipulating the volume;
var sTransform1:SoundTransform = new SoundTransform(1,0);
// The sound starts not muted
var Mute1:Boolean = true;
var vol:Number = 0;

sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
// Set the sound volume;
function setMute1(vol)
{
    sTransform1.volume = vol;
    SoundMixer.soundTransform = sTransform1;
    // Check if sound is muted
    if (vol<=0)
    {
        Mute1 = true;
    }
    else
    {
        Mute1 = false;
    }
}
// Start/stop sound
function startOrStop()
{
    if (Mute1 === false)
    {
        myChannel.stop();
        setMute1(0);
    }
    else
    {
        setMute1(1);
        myChannel = mySound.play();
    }
}
// This happens when you click the buttom
function toggleMuteBtn1(event:Event)
{
    startOrStop()
}

In actionscrip 2 there was a function that would stop all sounds, in actionscript 3 you can't do that anymore, but you can still assign sounds to frames.

Upvotes: 5

user2650087
user2650087

Reputation: 76

This example mutes and unmutes the sound. The sound is't stopped, just muted. Also here the sound must be assigned in the code, and not to the frame.

// load the sound
var mySound:Sound = new Sound();
mySound.load(new URLRequest("loop.mp3"));
var myChannel:SoundChannel = new SoundChannel();
// tool you need for manipulating the volume;
var sTransform1:SoundTransform = new SoundTransform(1,0);
// The sound starts not muted
var Mute1:Boolean = true;
var vol:Number = 0;

sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
// Set the sound volume;
function setMute1(vol)
{
    sTransform1.volume = vol;
    SoundMixer.soundTransform = sTransform1;
    // Check if sound is muted
    if (vol<=0)
    {
        Mute1 = true;
    }
    else
    {
        Mute1 = false;
    }
}
// Toggle mute on/off
function toggleMute()
{
    if (Mute1 === false)
    {
        setMute1(0);
    }
    else
    {
        setMute1(1);
    }
}
// This happens when you click the buttom
function toggleMuteBtn1(event:Event)
{
    // if not playing, the sound
    if (myChannel.position != 0) {
    } else {
        myChannel = mySound.play();
    }

    toggleMute();
}

Upvotes: 1

Related Questions