2
我試圖按照韋斯博斯的Javascript30教程,但是當我試圖打出了「的Javascript鼓組」的網站,我不能得到任何聲音播放。適當的聲音文件是有的,但是當我按下鍵,嘗試播放聲音,當我檢查控制檯出現此錯誤消息:調用.play()創建「未捕獲(在promise中)DOMException:該元素沒有支持的源。」錯誤
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()工作?
您使用哪種瀏覽器?也許你的瀏覽器缺少支持?您還應該檢查瀏覽器支持的格式,例如在這裏https://developer.mozilla.org/en-US/docs/Web/HTML/Supported_media_formats –