Using Javascript to Stop an Audio

Hi,

I added the following javascript to a button to play audio. But I don’t know how to use another button (or preferably the very same button) to stop this audio. I don’t know much coding at all but I think my problem is that I don’t know how to reference this audio file to use pause() in another workflow without messing with HTML…

Wondering if there is an easy way to achieve this?

Thanks a million,
Max

image

I honestly don’t advise you run such a code using that method. You can use a plugin like howler to achieve this. However here’s a solution if you wish to continue with this.
Instead of using:
const music , use window.music
This way the “music” variable becomes global.
Since “music” is now global. You can easily run another JavaScript code like this:
window.music.pause()

I ended up using the Howler Plugin. Works well for my purpose.

But yes, will definitely try the code you suggested for future reference. I was going around StackOverflow to find what I was looking for but couldn’t since I am separating HTML and Javascript.

Hopefully, I will start to learn some real coding soon - seems counterintuitive but the more I use no-code the more I get interested in coding.

Thanks so much for your answer, KingJoseph!

This topic was automatically closed after 70 days. New replies are no longer allowed.