Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Canvass: How to listen for the end of a sound event

Participant ,
Oct 31, 2021 Oct 31, 2021

Hi. I finally got the first part of this working. 

I am trying to check for end of a sound.

However, all the examples I see don't seem to work. 

 

The console says I can't use .on

 

 

this.btn_play.addEventListener("click", buttonClicked.bind(this));
function buttonClicked () {

var instance = createjs.Sound.play("itemName"); 
//instance.on("complete", this.handleComplete, this);

if (itemName=="monkey") {
monkeyClickSound.play("monkeySound");
completeMonkeySound = monkeyClickSound.play("monkeySound");
completeMonkeySound.on("complete", this.handleComplete, this);
}

if (itemName=="froggy") {
froggyClickSound.play("froggySound");
}


this.genie.visible=true;
this.genie_static.visible=false;

//monkeyClickSound.on("complete", handleComplete, null, true);
}

function handleComplete() {
console.log("sound complete works");
this.genie.visible=false;
this.genie_static.visible=true;
}

 

 

321
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 31, 2021 Oct 31, 2021

Hi.

 

This approach should work:

function soundButtonHandler()
{
	var sound = createjs.Sound.play("LibSoundLinkage", { volume: 0.2 });
	sound.on("complete", soundCompleteHandler, this);
}

function soundCompleteHandler()
{
	alert("Sound complete.");
}

this.soundButton.on("click", soundButtonHandler, this);

 

Please notice that the code of your first commented line should be:

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

 

And not:

// handleComplete is a function and not a method so the 'this' 
...
Translate
Community Expert ,
Oct 31, 2021 Oct 31, 2021

Hi.

 

This approach should work:

function soundButtonHandler()
{
	var sound = createjs.Sound.play("LibSoundLinkage", { volume: 0.2 });
	sound.on("complete", soundCompleteHandler, this);
}

function soundCompleteHandler()
{
	alert("Sound complete.");
}

this.soundButton.on("click", soundButtonHandler, this);

 

Please notice that the code of your first commented line should be:

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

 

And not:

// handleComplete is a function and not a method so the 'this' keyword is not used
instance.on("complete", this.handleComplete, this);

 

I hope this helps.

Regards,

JC

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 31, 2021 Oct 31, 2021

Hi thank you for answering so quickly and on a Sunday.

 

I have added your code but I don't know how to connect my first parts of code to yours. This is how I have things at the moment. I don't understand libSoundLinkage very well.

 

//Play sounds
var preload = new createjs.LoadQueue();
preload.addEventListener("fileload", handleFileComplete);
preload.loadFile ("sounds/monkey.mp3", "itemSound");

//Register sounds
function handleFileComplete (event) {
	createjs.Sound.registerSound("sounds/monkey.mp3", "monkeySound");
	createjs.Sound.registerSound("sounds/froggy.mp3", "froggySound");
	
//Prepare sounds
	monkeyClickSound = createjs.Sound.play("monkeySound");
	froggyClickSound = createjs.Sound.play("froggySound");
}

this.soundButton.on("click", soundButtonHandler, this);

function soundButtonHandler()
{
	var sound = createjs.Sound.play("LibSoundLinkage", { volume: 0.2 });
	sound.on("complete", soundCompleteHandler, this);
}

function soundCompleteHandler()
{
	alert("Sound complete.");
	this.genie.visible=false;
	this.genie_static.visible=true;
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 31, 2021 Oct 31, 2021

Your answer was great. I changed one line of code and it all worked so well.

 

//var sound = createjs.Sound.play("LibSoundLinkage", { volume: 0.2 });
var sound = createjs.Sound.play(itemName);

 

itemName being froggy, monkey etc... which I get from the drag target

itemName = evt.currentTarget.name;

 

BUT I'm very interested to know what 

"LibSoundLinkage"

the following means, just to see if I am doing things properly. I mean it works but it may be a bad way of doing things. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 31, 2021 Oct 31, 2021

Great!

 

It's a wise approach to use the current target name as the linkage.

 

"LibSoundLinkage" is just a generic suggestion for linkage names in the Library. It's not something special. Don't worry.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 31, 2021 Oct 31, 2021
LATEST

Thank you so much.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines