JavaScript play() bug in Safari 15

Hi!

Does anyone have a good solution for the following:

I want to use JavaScripts HTMLMediaElement.play() method to play a sound when a button is clicked. Everything working fine in Chrome and Firefox, but Safari 15... Every time I click the button there is some kind of delay or it is not even played. I used the code as described below:

const mainButton = document.getElementById('main-button');
const buttonSound = new Audio (button-sound.mp3);

mainButton.addEventListener('click', e => {
  buttonSound.play();
});

It would be great if someone could help!

JavaScript play() bug in Safari 15
 
 
Q