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

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


当前回答

我在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浏览器,由于网站默认的“自动播放”选项是“带声音停止媒体”,这种策略也会带来权限问题。

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

其他回答

其他的答案都对我没用。 我的解决办法是触发点击视频本身;Hacky(因为需要超时),但它工作正常:

function startVideoIfNotStarted () {
    $(".id_of_video_tag").ready(function () {
        window.setTimeout(function(){
            videojs("id_of_video_tag").play()
        }, 1000);
    });
}
$(startVideoIfNotStarted);

一开始我只能播放所有可见的视频,但旧手机的表现不太好。现在我播放离窗口中心最近的视频,然后暂停其他视频。香草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();
}

花了两个小时尝试上面提到的所有解决方案。

这是最后对我有用的方法:

var vid = document.getElementById("myVideo");
vid.muted = true;

在Safari iOS上没有任何效果,直到我添加了playsinline属性到视频中

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