Ask coding questions

← Back to all posts
2 issues with audio
h
Coder2195Text (49)

Scratch provides a “start sound” block
How do I simulate that in JS

Also, what event listener is the one to check when audio FULLY loaded?

Comments
hotnewtop
Coder100 (18109)

well
the classic way would be to create an audio element
then .play() it.

if all else fails, you could resort to a library
https://howlerjs.com/

BrysonVan1 (11)

1). Start Sound with JS - try looking at this w3schools tutorial. I've used this in the past and it works pretty well. https://www.w3schools.com/graphics/game_sound.asp
2). const audio =//get audio by ID or Class, etc audio.addEventListener("load",() => { //Audio is loaded... })

Coder2195Text (49)

@BrysonVan1 start sounds like it can be repeated multiple times at once

BrysonVan1 (11)

@Coder2195Text Sorry, what can be repeated multiple times at once?

Coder2195Text (49)

@BrysonVan1 the sound
Like scratch start sound block

BrysonVan1 (11)

@Coder2195Text I guess the only way you could do it would be to initialize as many as you wanted to play at the same time initially then play each one at the same time. If you did the .play() multiple times it would just restart the sound. There's not really a great total replacement for the scratch start block that I know of at least.

Coder2195Text (49)

@BrysonVan1 but how does scratch do it then

Coder2195Text (49)

@BrysonVan1 I found that I need to use cloneNode
But does cloneNode require extra loading of the sound afterwards