youtube.com上的video如何在iOS上自动播放

当我在我的网站上将YouTubevideo(IFrame API)的自动播放设置为true时,它不起作用。 所有的文件说,苹果因带宽原因禁用ios的自动播放。

不过,我发现iPad上的youtube.com会自动播放video(无需点击video元素)。 YouTube如何运作? 很难想象苹果会为YouTube做些特别的事情。

看起来,YouTube的团队使用用户的互动,当点击/点击一个videobutton授权复制。

请记住,youtube.com是SPA(单页应用程序),所以没有页面重新加载或redirect。

所以,youtube.com没有启用自动播放function。

你可以用javascript来做到这一点。

这里是一个例子:

<div class="video-container"> <div id="player"> </div> <!--<iframe width="960" height="720" src="//www.youtube.com/embed/5YptIh_avrM?rel=0&autoplay=1" frameborder="0" allowfullscreen></iframe>--> </div> <script> // 2. This code loads the IFrame Player API code asynchronously. var tag = document.createElement('script'); tag.src = "https://www.youtube.com/iframe_api"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); // 3. This function creates an <iframe> (and YouTube player) // after the API code downloads. var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { height: '720', width: '960', videoId: 'YOUR_ID_HERE', events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } // 4. The API will call this function when the video player is ready. function onPlayerReady(event) { event.target.playVideo(); event.target.setVolume(20); } // 5. The API calls this function when the player's state changes. // The function indicates that when playing a video (state=1), // the player should play for six seconds and then stop. var done = false; function onPlayerStateChange(event) { } function stopVideo() { player.stopVideo(); } </script> 

我还没有testing过这个,但是你可以使用Javascript并且调用播放器的播放function,甚至在页面加载的时候点击元素。

这是不能做到的。 由于各种原因(包括但不限于数据使用),Apple不允许自动播放video。

请参阅:

Youtubeembedded式video:自动播放function不工作在iPhone