Calling .play() creates "Uncaught (in promise) DOMException: The element has no supported sources." error

Viewed 8185

I'm trying to follow Wes Bos's Javascript30 tutorials, but when I tried making the "Javascript Drum Kit" site, I couldn't get any sound to play. The appropriate sound files are there, but when I press the keys to try to play the sound, this error message appears when I inspect the console:

jsdrumkit.html:66 - Uncaught (in promise) DOMException: The element has no supported sources.

This is the Javascript of the site:

function playSound(e){
    //querySelector() is just when you need a single return value
    //audio[input] is an attribute selector, and it works just like its CSS counterpart.
    const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
    const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
    if(!audio) return; 
    audio.currentTime = 0; //rewind the file to the start

    audio.play(); //**line 66 in the site's code**
    console.log(key);
    key.classList.toggle('playing');
}

function removeTransition(e) {
    if(e.propertyName !== 'transform') return; //skip if it's not a transform
    this.classList.remove('playing');
}

const keys = document.querySelectorAll('.key');
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
window.addEventListener('keydown', playSound);

What am I missing if I can't even get .play() to work?

1 Answers
Related