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

<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。


当前回答

对于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() 
​ } 

其他回答

我们最近用一个嵌入式视频解决了一个类似的问题,发现自动播放和静音属性对我们的实现来说不够。

我们在代码中添加了第三个“playsinline”属性,它为iOS用户解决了这个问题。

此修复是特定于视频要内联播放。来自https://webkit.org/blog/6784/new-video-policies-for-ios/:

在iPhone上,元素现在将允许内联播放,并且在播放开始时不会自动进入全屏模式。 没有playsinline属性的元素将继续需要全屏模式在iPhone上播放。 当用缩放手势退出全屏时,没有playsinline的元素将继续inline播放。

我能得到的最好的修复是在</video>之后添加这段代码

<script>
    document.getElementById('vid').play();
</script>

...虽然不漂亮,但还是管用的。

更新 最近许多浏览器只能自动播放视频的声音关闭,所以你需要添加静音属性的视频标签

<video autoplay muted>
...
</video>

试试这个:

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

我通常都是这么做的。循环、控件和自动播放不需要值,它们是布尔属性。

在使用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.

一开始我只能播放所有可见的视频,但旧手机的表现不太好。现在我播放离窗口中心最近的视频,然后暂停其他视频。香草JS。你可以选择你喜欢的算法。

//slowLooper(playAllVisibleVideos);
slowLooper(playVideoClosestToCenter);

function isVideoPlaying(elem) {
    if (elem.paused || elem.ended || elem.readyState < 2) {
        return false;
    } else {
        return true;
    }
}
function isScrolledIntoView(el) {
    var elementTop = el.getBoundingClientRect().top;
    var elementBottom = el.getBoundingClientRect().bottom;
    var isVisible = elementTop < window.innerHeight && elementBottom >= 0;
    return isVisible;
}
function playVideoClosestToCenter() {
    var vids = document.querySelectorAll('video');
    var smallestDistance = null;
    var smallestDistanceI = null;
    for (var i = 0; i < vids.length; i++) {
        var el = vids[i];
        var elementTop = el.getBoundingClientRect().top;
        var elementBottom = el.getBoundingClientRect().bottom;
        var elementCenter = (elementBottom + elementTop) / 2.0;
        var windowCenter = window.innerHeight / 2.0;
        var distance = Math.abs(windowCenter - elementCenter);
        if (smallestDistance === null || distance < smallestDistance) {
            smallestDistance = distance;
            smallestDistanceI = i;
        }
    }
    if (smallestDistanceI !== null) {
        vids[smallestDistanceI].play();
        for (var i = 0; i < vids.length; i++) {
            if (i !== smallestDistanceI) {
                vids[i].pause();
            }
        }
    }
}
function playAllVisibleVideos(timestamp) {
    // This fixes autoplay for safari
    var vids = document.querySelectorAll('video');
    for (var i = 0; i < vids.length; i++) {
        if (isVideoPlaying(vids[i]) && !isScrolledIntoView(vids[i])) {
            vids[i].pause();
        }
        if (!isVideoPlaying(vids[i]) && isScrolledIntoView(vids[i])) {
            vids[i].play();
        }
    }
}
function slowLooper(cb) {
    // Throttling requestAnimationFrame to a few fps so we don't waste cpu on this
    // We could have listened to scroll+resize+load events which move elements
    // but that would have been more complicated.
    function repeats() {
        cb();
        setTimeout(function() {
            window.requestAnimationFrame(repeats);
        }, 200);
    }
    repeats();
}