【问题标题】:Calling .play() creates "Uncaught (in promise) DOMException: The element has no supported sources." error调用 .play() 会创建“未捕获(承诺中)DOMException:该元素没有支持的来源。”错误
【发布时间】:2022-01-28 13:04:15
【问题描述】:

我正在尝试遵循 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() 工作,我错过了什么?

【问题讨论】:

标签: javascript audio


【解决方案1】:

尝试在 Firefox 中播放。去年对 Chrome 的更新在涉及承诺、部分内容等时导致播放音频出现问题。我正在研究解决方案,但目前还没有找到,这里是信息:Audio from Icecast server not playing in Chrome

【讨论】:

    猜你喜欢
    • 2017-02-03
    • 2019-11-03
    • 2017-10-02
    • 2019-09-08
    • 2018-06-25
    • 2019-05-27
    • 2022-10-20
    • 2019-07-22
    • 1970-01-01
    相关资源
    最近更新 更多