Skip to main content
Inspiring
May 3, 2020
Answered

Fade out mp3

  • May 3, 2020
  • 19 replies
  • 2123 views

HTML5 canvas how do I do that so I fade out with single mp3 sound.createjs

This topic has been closed for replies.
Correct answer kglad

I have this at the beginning of the frame of the slideshow:

 

createjs.Sound.on("fileload", handleFileLoad1);
createjs.Sound.registerSound("music1.mp3", "MySound1");

function handleFileLoad1()
{
createjs.Sound.play("MySound1");
}

but now several frames ahead I want to fade out the sound because I don't want a button. How do I do that?


assign a reference to your sound (when it's created) and start the fade when you want.

19 replies

JoãoCésar17023019
Community Expert
Community Expert
February 6, 2024

Hi.

 

Just adding that you can also use the Tween class to fade a sound. Like this:

var sound;

this.yourButton.on("click", function()
{
	if (sound)
		sound.stop();
	
	sound = createjs.Sound.play("YourSoundLinkage");
	createjs.Tween.get(sound).wait(2000).to({ volume: 0 }, 350);
}, this);

 

Regards,

JC

kglad
Community Expert
Community Expert
May 4, 2020
this.s.addEventListener("click",f.bind(this));
this.fadeOut = fadeOutF.bind(this);


function f(){
	this.sound = createjs.Sound.play("soundID");
	this.addEventListener("tick", this.fadeOut);
}

function fadeOutF(){
	this.sound.volume -= .02;
	if(this.sound.volume <= 0){
		this.removeEventListener("tick", this.fadeOut);
	}
}
peorAuthor
Inspiring
May 4, 2020

Thank you for helping me show code examples. Now I can add it to my slideshow.

kglad
Community Expert
Community Expert
May 4, 2020

you're welcome.

kglad
Community Expert
Community Expert
May 4, 2020

start a loop (eg, tick) and adjust the sound's volume in the listener function.

peorAuthor
Inspiring
May 4, 2020

Can you show me an example with code how I do it.