Skip to main content
Participating Frequently
January 14, 2018
Answered

Mute and Unmute HTML5 Canvas Click Event in Adobe Animate CC

  • January 14, 2018
  • 3 replies
  • 4296 views

Hi,

I am trying to get two buttons to perform two different functions. I have been searching the web and YouTube all day with no luck.

Button 1 is to mute/stop a .mp3 (looped) and Button 2 is to unmute/stop the aforementioned .mp3

Nothing I have tried has worked. This was the most promising solution I could find but I could not get it to work:

this.SoundOn.addEventListener("click", silence.bind(this));

function silence()

{

  if(!mcSndVar) {

  sound_1.volume = 0;

  mcSndVar = true; }

  else {

  sound_1.volume = 1;

  mcSndVar = false;

}

}

Any ideas?

This topic has been closed for replies.
Correct answer albertd9194959

Forgive my lack of skill and understanding, I am new to this, can you explain where I am going wrong?

This is what I tried:

this.stop();

this.SoundOn.visible = true;

this.SoundOff.visible = false;

this.SoundOn.addEventListener("click", ClickToHideOn.bind(this));

this.SoundOn.addEventListener("click", ClickToShowOn.bind(this));

this.SoundOff.addEventListener("click", ClickToHideOff.bind(this));

this.SoundOff.addEventListener("click", ClickToShowOff.bind(this));

this.SoundOn.addEventListener("click", silence.bind(this));

this.SoundOff.addEventListener("click", serenade.bind(this));

this.SoundOff.addEventListener("click", loadAudio.bind(this));

createjs.Sound.on("fileload", this.loadAudio, this);

createjs.Sound.registerSound("sounds/PeterGunderMovement1.mp3", "SoundOn");

var SoundOn = createjs.Sound.createInstance("SoundOn");

createjs.Sound.registerSound("sounds/PeterGunderMovement1.mp3", "SoundOff");

var SoundOff = createjs.Sound.createInstance("SoundOff");

function loadAudio(event) {

  var instance = createjs.Sound.play("sound");

  instance.on("complete", this.handleComplete. this);

  instance.volume = 0.5;

}

function ClickToHideOn()

{

  this.SoundOn.visible = false;

}

function ClickToShowOn()

{

  this.SoundOff.visible = true;

}

function ClickToHideOff()

{

  this.SoundOff.visible = false;

}

function ClickToShowOff()

{

  this.SoundOn.visible = true;

}

function silence()

{

  SoundOn.paused = true;

}

function serenade()

{

  SoundOn.play();

  SoundOff.paused = false;

}


Hi Thad, your getting there.... you need to make sure you don't have any sound in your timeline layers.

You only need 1 event listener per button ....you only need to register one sound, as you pause, play that particular sound, unless you have a specific SoundOff sound....

this.stop();

// Register all our sounds here, as well as the fileload event.

createjs.Sound.registerSound("sounds/PeterGunderMovement1.mp3", "SoundOn");

createjs.Sound.addEventListener("fileload", handleFileLoad);

// Create all our variables here to be called later.

var SoundOn = createjs.Sound.createInstance("SoundOn");

// Set the button visibility states.

this.SoundOn.visible = true;

this.SoundOff.visible = false;

// Add the even listeners for our buttons.

this.SoundOn.addEventListener("click", silence.bind(this));

this.SoundOff.addEventListener("click", serenade.bind(this));

function handleFileLoad(event)

{

// Once sound has loaded, start playing audio, you can also use this.play(); if you have animations.

console.log("sound loaded");

createjs.Sound.removeEventListener("fileload", handleFileLoad);

SoundOn.play();

SoundOff.paused = false;

}

function silence()

{

// the button called SoundOff will trigger this

console.log("Button Pause Sound");

this.SoundOn.visible = false;

this.SoundOff.visible = true;

SoundOn.paused = true;

}

function serenade()

{

// the button called SoundOnwill trigger this

console.log("Button Play Sound");

this.SoundOn.visible = true;

this.SoundOff.visible = false;

SoundOn.play();

SoundOff.paused = false;

}

3 replies

albertd9194959
Inspiring
January 15, 2018

Hi Thad,

In addition to the above: For more advanced sound manipulation within canvas you need to use the soundJS class.

SoundJS v1.0.0 API Documentation : SoundJS

In its very basic form, it would look like:

createjs.Sound.registerSound("sounds/button1Sound.mp3", "buttonOneSound");

var soundOne = createjs.Sound.createInstance("buttonOneSound");

createjs.Sound.registerSound("sounds/button2Sound.mp3", "buttonTwoSound");

var soundTwo = createjs.Sound.createInstance("buttonTwoSound");

You can then control, stop, pause, play, seek volume, loop etc etc.

soundOne.play();

soundTwo.stop();

OR use the following if you dont want to lose the current sound play location.

soundOne.paused = false;

soundTwo.paused = true;

Regards,

Participating Frequently
January 15, 2018

I will give this a try.

Community Expert
January 14, 2018
Legend
January 14, 2018

I can't tell if you're using "mute" and "stop" interchangeably (they aren't), or if you mean you want to mute and stop a sound, which seems a bit redundant.