video自动播放功能,却遭遇google浏览器拦截:play() failed because the user didn‘t interact with the document first

前言

项目需求:希望宣传页上,循环播放视频动画效果,设计师提供了一个mp4给我,可是当我将其放在首页后,却遇到如下错误:

play() failed because the user didn't interact with the document first

视频播放代码为:

<video src="~examples/assets/images/banner.mp4" autoplay="autoplay" loop="loop" id="video"></video>

解决问题

通过翻墙,查阅Chrome开发网站,得出以下2点结论:

  1. 无声的视频总是能自动播放
  2. 最上层的frames能给视频源所在的iframes授权:允许自动有声播放

原文如下所示:

Chrome’s autoplay policies are simple:

  • Muted autoplay is always allowed.
  • Autoplay with sound is allowed if:
    • The user has interacted with the domain (click, tap, etc.).
    • On desktop, the user’s Media Engagement Index threshold has been crossed, meaning the user has previously played video with sound.
    • The user has added the site to their home screen on mobile or installed the PWA on desktop.
  • Top frames can delegate autoplay permission to their iframes to allow autoplay with sound.

像我前言提到的,公司的宣传页上需要初次加载完成后,便能自动循环播放,所以用户必须有交互这一点不可取,于是我采用第一点,将video代码改为如下所示,即可自动循环播放了

<video src="~examples/assets/images/banner.mp4" autoplay="autoplay" loop="loop" muted="muted"></video>
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值