我正在尝试遵循 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);
Run Code Online (Sandbox Code Playgroud)
如果我什至无法让 .play() 工作,我会错过什么?
尝试在 Firefox 中播放。去年 Chrome 的更新在涉及承诺、部分内容等时导致播放音频出现问题。我正在研究解决方案,但目前尚未找到解决方案,以下信息:来自 Icecast 服务器的音频未在 Chrome 中播放
归档时间: |
|
查看次数: |
9210 次 |
最近记录: |