音频不工作
Audio onended not functioning
当用户与 window 交互时播放音频,但是当音频结束时它不会记录 'audio ended'
let audio = new Audio();
window.onclick = () => {
if (audio.paused) pickMusic().play();
};
audio.onended = () => {
console.log("audio ended");
};
function pickMusic() {
switch (Math.floor(Math.random() * 1)) {
case 0:
return (audio = new Audio("/music/gorp.mp3"));
break;
case 1:
return (audio = new Audio("/music/gorp.mp3"));
break;
}
}
<p>Click anywhere!</p>
当您调用 new Audio()
时,它将覆盖之前已附加到 onended
事件的音频。
在这种情况下,无论何时调用new Audio()
,您还需要再次初始化onended
事件
let audio = new Audio()
window.onclick = () => {
if (audio.paused) pickMusic().play();
};
function pickMusic() {
switch (Math.floor(Math.random() * 1)) {
case 0:
audio = new Audio("http://commondatastorage.googleapis.com/codeskulptor-assets/jump.ogg")
audio.onended = () => {
console.log("audio ended");
};
return audio;
case 1:
audio = new Audio("http://commondatastorage.googleapis.com/codeskulptor-assets/week7-brrring.m4a")
audio.onended = () => {
console.log("audio ended");
};
return audio;
}
}
<p>Click anywhere!</p>
当用户与 window 交互时播放音频,但是当音频结束时它不会记录 'audio ended'
let audio = new Audio();
window.onclick = () => {
if (audio.paused) pickMusic().play();
};
audio.onended = () => {
console.log("audio ended");
};
function pickMusic() {
switch (Math.floor(Math.random() * 1)) {
case 0:
return (audio = new Audio("/music/gorp.mp3"));
break;
case 1:
return (audio = new Audio("/music/gorp.mp3"));
break;
}
}
<p>Click anywhere!</p>
当您调用 new Audio()
时,它将覆盖之前已附加到 onended
事件的音频。
在这种情况下,无论何时调用new Audio()
,您还需要再次初始化onended
事件
let audio = new Audio()
window.onclick = () => {
if (audio.paused) pickMusic().play();
};
function pickMusic() {
switch (Math.floor(Math.random() * 1)) {
case 0:
audio = new Audio("http://commondatastorage.googleapis.com/codeskulptor-assets/jump.ogg")
audio.onended = () => {
console.log("audio ended");
};
return audio;
case 1:
audio = new Audio("http://commondatastorage.googleapis.com/codeskulptor-assets/week7-brrring.m4a")
audio.onended = () => {
console.log("audio ended");
};
return audio;
}
}
<p>Click anywhere!</p>