我花了很多时间试图弄清楚为什么视频会像这样嵌入:

<video height="256" loop autoplay muted controls id="vid">
         <source type="video/mp4" src="video_file.mp4"></source>
         <source type="video/ogg" src="video_file.ogg"></source>
</video>

一旦页面在FireFox中加载,就开始自动播放,但不能在基于Webkit的浏览器中自动播放。这只发生在一些随机的页面上。到目前为止我还没有找到原因。我怀疑一些未关闭的标签或CMS编辑器创建的大量JS。


当前回答

在使用jQuery play()或DOM操作后,根据其他答案的建议,它仍然不能工作(视频不能自动播放)在Chrome for Android(版本56.0)。

根据developers.google.com上的这篇文章,从Chrome 53开始,如果视频被静音,浏览器会尊重自动播放选项。

因此,在视频标签中使用自动播放静音属性可以使视频在Chrome浏览器中自动播放。

摘自上述链接:

Muted autoplay for video is supported by Chrome for Android as of version 53. Playback will start automatically for a video element once it comes into view if both autoplay and muted are set[...] <video autoplay muted> <source src="video.webm" type="video/webm" /> <source src="video.mp4" type="video/mp4" /> </video> Muted autoplay is supported by Safari on iOS 10 and later. Autoplay, whether muted or not, is already supported on Android by Firefox and UC Browser: they do not block any kind of autoplay.

其他回答

这是因为现在chrome是阻止自动播放html5视频,所以默认情况下,他们将不允许自动播放。所以我们可以使用chrome标志设置来改变这个设置。这是不可能的正常情况下,所以我找到了另一个解决方案。这是完美的… (添加预加载=“汽车”)

<video autoplay preload="auto" loop="loop" muted="muted" id="videoBanner" class="videoBanner">
<source src="banner-video.webm" type="video/webm">
<source src="banner-video.mp4" type="video/mp4">
<source src="banner-video.ogg" type="video/ogg">

var herovide = document.getElementById('videoBanner');
       herovide.autoplay=true;
       herovide.load();  

对于angular,你必须静音它,并像下面这样在ngAfterViewInit()中播放它

<video height="256" loop autoplay muted controls id="vid" #videoRef>
         <source type="video/mp4" src="video_file.mp4"></source>
         <source type="video/ogg" src="video_file.ogg"></source>
</video>
 ​@ViewChild('videoRef', { static: true }) videoRef!: ElementRef

​ngAfterViewInit(): void {
  ​const media = this.videoRef.nativeElement
  ​media.muted = true 
  ​media.play() 
​ } 

在使用jQuery play()或DOM操作后,根据其他答案的建议,它仍然不能工作(视频不能自动播放)在Chrome for Android(版本56.0)。

根据developers.google.com上的这篇文章,从Chrome 53开始,如果视频被静音,浏览器会尊重自动播放选项。

因此,在视频标签中使用自动播放静音属性可以使视频在Chrome浏览器中自动播放。

摘自上述链接:

Muted autoplay for video is supported by Chrome for Android as of version 53. Playback will start automatically for a video element once it comes into view if both autoplay and muted are set[...] <video autoplay muted> <source src="video.webm" type="video/webm" /> <source src="video.mp4" type="video/mp4" /> </video> Muted autoplay is supported by Safari on iOS 10 and later. Autoplay, whether muted or not, is already supported on Android by Firefox and UC Browser: they do not block any kind of autoplay.

我在iphone的Safari上播放视频时遇到了一个问题。在video标签中添加playsinline属性可以解决这个问题,而且它是有效的!

<video autoplay muted loop playsinline class="someClass">
  <source src="source.mp4" type="video/mp4">
</video>

你也会在Safari和OSX上遇到这个问题,如果你对这个属性playsinline感到困惑,这里是解释。

移动浏览器,playsinline将播放视频的正确位置,而不是默认的全屏播放。

对于OSX上的Safari浏览器,由于网站默认的“自动播放”选项是“带声音停止媒体”,这种策略也会带来权限问题。

这就是为什么我们需要属性静音。

在safari iPhone上,当电池电量不足,iPhone处于低电量模式时,它不会自动播放,即使你在视频html标签上设置了以下属性:自动播放,循环,静音,playsinline。

走一走我发现工作是有用户手势事件触发视频播放:

document.body.addEventListener("touchstart", function () {
    var allVideos = document.querySelectorAll('video');
    for (var i = 0; i < allVideos.length; i++) {
        allVideos[i].play();
    }
},{ once: true });

你可以在webkit网站上阅读更多关于iOS用户手势和视频策略的信息:

https://webkit.org/blog/6784/new-video-policies-for-ios/