2016-12-28 116 views
0

我在我的網站olegefimkin.ru上有背景視頻,應該在頁面加載時自動播放。它可以在PC和某些手機上使用,但不適用於某些手機和平板電腦(例如iPhone)。 我使用這個網站碼:如何在手機和平​​板電腦上製作html視頻自動播放?

<video autoplay loop muted id="main-video"> 
    <source src="/themes/basic/video/intro.webm" type="video/webm"></source> 
    <source src="/themes/basic/video/intro.mp4" type="video/mp4"></source> 
</video> 

我也試着添加JavaScript video.play(),但它仍然無法在Safari工作。 如何使移動設備上的視頻自動播放?

回答

2

視頻不會在Safari IOS自動播放,除非你滿足以下要求:

<video> elements will be allowed to autoplay without a user gesture if their source media contains no audio tracks. 
<video muted> elements will also be allowed to autoplay without a user gesture. 
If a <video> element gains an audio track or becomes un-muted without a user gesture, playback will pause. 
<video autoplay> elements will only begin playing when visible on-screen such as when they are scrolled into the viewport, made visible through CSS, and inserted into the DOM. 
<video autoplay> elements will pause if they become non-visible, such as by being scrolled out of the viewport. 

你需要無論是從源中刪除音頻或靜音它,使聲音通過手勢來激活。

Webkit policies for video

+0

我很好奇聽到這個答案是否有幫助。我已經滿足了所有這些要求,但仍然無法實現。我在Final Cut中創建了視頻,在時間線上分離並刪除了音軌,將其僅導出爲視頻(1.3mb mp4),並在視頻標籤中指定了播放自動播放靜音。除非我直接將視頻放入瀏覽器的地址欄,否則我甚至無法在iOS 10.2上播放它。 – Mark

+0

你可以把你的代碼放入jsfiddle嗎? –

+0

這只是html代碼,沒有js甚至css。我會將其粘貼到下面的答案中。 – Mark

0
<video poster="video-sg.jpg" playsinline autoplay muted loop> 
    <source src="caffe.webm" type="video/webm"> 
    <source src="caffe.mp4"" type="video/mp4"> 
    Your Browser May Not Support This 
</video> 

在任何桌面最近瀏覽器,它自動播放和循環,因爲它應該。但在移動Safari中,它不會自動播放,甚至無法播放。我甚至不能點擊播放按鈕來播放它。只是海報圖像顯示。

道歉,如果我劫持OP的問題,但至少這是同一個問題!