【发布时间】:2021-04-21 13:57:39
【问题描述】:
当我尝试使用下面的 Js 在我的 chrome book 上播放音频时,我不断收到此消息:
“DOMException: play() 失败,因为用户没有先与文档交互。”
<script>
window.addEventListener('load', (event) => {
console.log('page is fully loaded');
console.log('playing music');
var audioElement = new Audio('indie.mp3').play();
if (audioElement !== undefined) {
audioElement.then(_ => {
// Autoplay started!
}).catch(error => {
// Autoplay was prevented.
// Show a "Play" button so that user can start playback.
console.log(error);
});
}
});
</script>
【问题讨论】:
标签: javascript audio