5

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?

CiaranC94
  • 176
  • 4
  • 20
  • Which browser are you using? Perhaps your browser is missing support? You should also check the supported formats of your browser, e.g. on here https://developer.mozilla.org/en-US/docs/Web/HTML/Supported_media_formats – Marc Scheib Jul 29 '17 at 11:21

1 Answers1

0

Try playing it in Firefox. An update to Chrome last year caused a problem with playing audio when it involves promises, partial content, etc. I'm working on a solution but currently haven't found one, here's info: Audio from Icecast server not playing in Chrome

Dan Mantyla
  • 1,840
  • 1
  • 22
  • 33