【问题标题】:Safari doesn't want to autoplay video. How to fix this?Safari 不想自动播放视频。如何解决这个问题?
【发布时间】:2020-02-23 13:32:38
【问题描述】:

我已经尝试了几乎所有方法,但找不到如何在网页开头制作 Safari 自动播放视频的解决方案。

<script>
	
		   var loopCount = 0;

		   document.getElementById('bgvid').addEventListener('timeupdate', function () {
		   	if (this.currentTime == 0)
		   	++loopCount;
		   	if (loopCount == 20)
		   	this.pause();
});

</script>

<script>
	
var promise = document.querySelector('#bgvid').play();

if (promise !== undefined) {
    promise.catch(error => {
        // Auto-play was prevented
        // Show a UI element to let the user manually start playback
    }).then(() => {
        // Auto-play started
    });
}

</script>

<script>	
	
	
	document.addEventListener('keydown', function(e) {
		if (e.keyCode == 113) {
			document.getElementById('bgvid').pause();
		}
	});
	
	document.addEventListener('keydown', function(e){
		if (e.keyCode == 112) {
			document.getElementById('bgvid').play(); 
		}		
	});
</script>

         
<!--------- Script Video End ---------->
#bgvid {
	object-fit: cover;
}

.video-container {
      position: absolute;
      width: 100%;
      height: 100vh;
      overflow: hidden;
}

video {
      object-fit: cover;
      width: 100vw;
      height: 100vh;
      position: absolute;
      top: 0;
      left: 0;
}

.video-container::after {
      content: "";
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100vh;
      background: #1b1b1b;
      opacity: 0.8;
}
      <a id="home"></a>
      <div class="wrapper">


            <!--------------- hero section starts here --------------->

            <div class="video-container">
	            
                  <video preload="metadata" poster="/img/prevideo1.jpg" autoplay   muted="true" loop playsinline id="bgvid">
                   
                    <source src="videobg%20-%20converted%20with%20Clipchamp.webm" type="video/webm" alt="Your browser does not support the video tag.">
                   
                   <source src="videobg.mp4" type="video/mp4" alt="Your browser does not support the video tag."> 
                    
					<p>Your browser does not support the video tag.</p>  
					    
                  </video>
            </div>

这里是页面链接:http://avelitest.epizy.com/index.html

但是,在 Chrome 中一切正常。我只是不明白它有什么问题。感谢您的帮助!)

【问题讨论】:

    标签: video safari html5-video autoplay


    【解决方案1】:

    “Safari...使用自动推理引擎来阻止大多数网站默认自动播放带有声音的媒体元素。”

    https://webkit.org/blog/7734/auto-play-policy-changes-for-macos/

    【讨论】:

    • 我已经尝试过了,并且我尝试使用属性“静音”,这也没有帮助。
    • 您已经尝试过什么?我链接到的页面说 Safari 会阻止自动播放。
    • 您写信给我说 Safari 会阻止“带有声音的媒体元素”自动播放,因为其中有声音,但在我的情况下,声音已关闭,并且此视频中根本没有声音。我的意思是我试图在 Safari 偏好设置中允许网站自动播放,但它仍然不会自动播放。
    • 对不起,不知怎的,我把你的问题误读为“Safari 不想自动播放音频”。然而,Safari 也会在未经明确许可的情况下阻止自动播放视频,正如文章所说:“网站应假定对
    • 好的,所以我尝试这样做,但它仍然不起作用。但是,当我直接从笔记本电脑而不是主机运行网站时,Safari 中一切正常。
    猜你喜欢
    • 2013-08-04
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多