我想在每次按下时立即触发声音。 (无需等待)。
我试图像那里那样做: Playing Sound Instantly (without waiting to complete) on Javascript
但是它不起作用。 (什么都没发生)
const SOUNDS = {
new: function (type) {
return {
CLAP: type === 'clap' ?
new Audio("https://s3-us-west-2.amazonaws.com/s.cdpn.io/1159990/clap.wav")
: undefined,
}
}
}
let App = {
initialize: function () {
DrumKit.listener();
},
listener: function () {
document.addEventListener('keydown', function (key) {
//CLAP
if (key.keyCode === 81) {
this.sound = SOUNDS.new.call(this, 'clap');
this.sound.play();
}
});
},
}
let DrumKit = Object.assign({}, App);
DrumKit.initialize();