Uncaught (in promise) DOMException: play() failed because the user didn‘t intera

211 阅读1分钟

原因:

浏览器必须在用户与页面进行交互后,才能进行播放音频

解决:

video标签设置muted属性,使它静音,这样视频就能自动播放了,但是没有声音。然后待用户在网页上有了任意触发后,再将muted去掉。或者让用户手动去打开音频

<video ref="video" :src="adUrl" :muted="muted" v-if="isVideo"></video>
data() {
    return {
        muted: true,
        isPlay: false
    }
}
this.autoPlay()
autoPlay() {
    if (this.isPlay) return
    this.isPlay = true
    // 静音状态下自动播放
    this.$refs.video.play()
    setTimeout(() => {
        // 取消静音
        this.muted = false
    }, 100)
}

参考链接:blog.csdn.net/weixin_4533…