我正在得到错误信息..

DOMException: play()失败,因为用户没有首先与文档交互。

..当使用Chrome 66版本在桌面上播放视频时。

我确实发现一个广告开始自动播放在一个网站上,但使用以下HTML:

<video
    title="Advertisement"
    webkit-playsinline="true"
    playsinline="true"
    style="background-color: rgb(0, 0, 0); position: absolute; width: 640px; height: 360px;"
    src="http://ds.serving-sys.com/BurstingRes/Site-2500/Type-16/1ff26f6a-aa27-4b30-a264-df2173c79623.mp4"
    autoplay=""></video>

那么,绕过Chrome v66的自动播放拦截器真的像添加webkit-playsinline="true", playsinline="true"和autoplay=""属性<video>元素一样简单吗?这有什么负面影响吗?


当前回答

我在尝试播放音频文件时遇到了类似的错误。起初,它是工作的,然后它停止工作时,我开始使用ChangeDetector的markForCheck方法在同一函数中触发重新呈现时,承诺解决(我有一个视图呈现的问题)。

当我改变了markForCheck detectChanges它又开始工作了。我真的无法解释发生了什么,我只是想把这个放在这里,也许它会帮助到某人。

其他回答

在地址栏中输入Chrome://标志

搜索:播放

播放政策 决定是否允许音频或视频时使用的策略 播放。 - Mac, Windows, Linux, Chrome OS, Android

将其设置为“不需要用户手势”

重新启动Chrome浏览器,你不需要改变任何代码

扩展DOM元素,处理错误,优雅地降级

下面我使用原型函数来包装原生DOM播放函数,获取它的承诺,然后在浏览器抛出异常时将其降级为播放按钮。这个扩展解决了浏览器的缺点,是即插即用的任何页面与目标元素的知识(s)。

// JavaScript
// Wrap the native DOM audio element play function and handle any autoplay errors
Audio.prototype.play = (function(play) {
return function () {
  var audio = this,
      args = arguments,
      promise = play.apply(audio, args);
  if (promise !== undefined) {
    promise.catch(_ => {
      // Autoplay was prevented. This is optional, but add a button to start playing.
      var el = document.createElement("button");
      el.innerHTML = "Play";
      el.addEventListener("click", function(){play.apply(audio, args);});
      this.parentNode.insertBefore(el, this.nextSibling)
    });
  }
};
})(Audio.prototype.play);

// Try automatically playing our audio via script. This would normally trigger and error.
document.getElementById('MyAudioElement').play()

<!-- HTML -->
<audio id="MyAudioElement" autoplay>
  <source src="https://www.w3schools.com/html/horse.ogg" type="audio/ogg">
  <source src="https://www.w3schools.com/html/horse.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>

公开chrome: / /设置/内容/声音 设置不需要用户手势 重新启动浏览器

尝试使用鼠标移动事件监听器

var audio = document.createElement("AUDIO")
document.body.appendChild(audio);
audio.src = "./audio/rain.m4a"

document.body.addEventListener("mousemove", function () {
    audio.play()
})

我发现最好的解决办法就是把视频静音

HTML

<video loop muted autoplay id="videomain">
  <source src="videoname.mp4" type="video/mp4">
</video>