我试图关注Wes Bos的Javascript30教程,但是当我尝试制作" Javascript Drum Kit"网站,我无法发挥任何声音。有合适的声音文件,但是当我按下键试图播放声音时,我检查控制台时会出现此错误消息:
jsdrumkit.html:66 - Uncaught (in promise) DOMException: The element has no supported sources.
这是该网站的Javascript:
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);
如果我甚至无法让.play()工作,我会错过什么?
答案 0 :(得分:1)
尝试在Firefox中播放。去年Chrome的更新导致播放音频时涉及承诺,部分内容等问题。我正在研究解决方案,但目前尚未找到一个,这里的信息:Audio from Icecast server not playing in Chrome