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

Event listener to detect when sound complete for HTML5 document

Explorer ,
Jun 14, 2022 Jun 14, 2022

Hi,

I've tried looking around for info on this, but I haven't had much luck. 

Is there an event listener to detect when a sound has completed playing?

 

To give some context to what I am trying to do.

I'm trying to play a visualizer animation that plays while a sound is playing. But I want the animation to stop when the sound file has completed playing.

 

Thanks in advance!

 

283
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
Explorer ,
Jun 14, 2022 Jun 14, 2022

Nevermind, I came across something that seems to be working for me.

Here's what I did, in case anyone else is looking for something similar:

 

_this.play_btn.on('click', function(){
	if (_this.bgm)
	_this.bgm.play();
	else
		_this.bgm = createjs.Sound.play("soundfile");
		_this.bgm.addEventListener("complete",stopvis.bind(this));
			function stopvis(){
			_this.visualizer.visible = false;
			}
});

 

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
LEGEND ,
Jun 14, 2022 Jun 14, 2022

If that can execute multiple times, it's going to keep adding more and more event listeners. You have to remove the event listener after it's done its job 

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
Explorer ,
Jun 14, 2022 Jun 14, 2022

Thanks for the heads up @ClayUUID 

 

Will this do the trick?

 

_this.play_btn.on('click', function(){
	if (_this.bgm)
	_this.bgm.play();
	else
		_this.bgm = createjs.Sound.play("soundfile");
		_this.bgm.addEventListener("complete",stopvis.bind(this));
			function stopvis(){
			_this.visualizer.visible = false;
			}
                _this.bgm.removeEventListener("complete",stopvis.bind(this));

});
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
LEGEND ,
Jun 14, 2022 Jun 14, 2022
LATEST

No, it absolutely will not, because bind() returns a new function reference every time it's called.

 

You can use the remove() method of the event object within an event handler to remove its associated listener.

https://createjs.com/docs/easeljs/classes/Event.html#method_remove

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