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

时间:2013-06-21 06:20:24

标签: ios ipad youtube youtube-api

当我在我的网站上为自己的YouTube视频(IFrame API)设置autoplay为true时,它不起作用。所有文档都说苹果因带宽原因禁用ios上的自动播放。

但是,我发现iPad上的youtube.com会自动播放视频(不会点击视频元素)。 YouTube是如何使其发挥作用的?很难想象Apple会为YouTube做些特别的事情。

5 个答案:

答案 0 :(得分:5)

似乎Youtube的团队在点击/点击视频按钮授权复制时会使用用户的互动。

请记住,youtube.com是SPA(单页应用程序),因此没有页面重新加载或重定向。

因此,youtube.com没有启用自动播放功能。

答案 1 :(得分:1)

您可以使用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>

答案 2 :(得分:0)

我偶然发现了类似但更复杂的问题/要求。

我需要在模式弹出窗口中显示YouTube视频。单击显示弹出窗口。 已获取YouTube API代码(来自https://developers.google.com/youtube/iframe_api_reference),并进行了一些修改以与Bootstrap弹出窗口配合使用。

下面是我的代码

    <div class="modal" id="videoModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="videoWrapper">
                    <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
                    <div id="player"> </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    // 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: '360',
            width: '640',
            videoId: 'YOUR VIDEO ID',
            events: {
                'onReady': onPlayerReady,
                'onStateChange': onPlayerStateChange
            }
        });
    }

    // 4. The API will call this function when the video player is ready.
    function onPlayerReady(event) {
        //event.target.playVideo();// Nullifying the action otherwise the video will play as soon as page loads

    }

    // 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) {
        if (event.data == YT.PlayerState.PLAYING && !done) {
            setTimeout(stopVideo, 6000);
            done = true;
        }
    }
    function stopVideo() {
        player.stopVideo();
    }

    function ShowPopup() {
        $('#videoModal').modal('show');
        player.playVideo();
    }
    //Stop video when bootstrap popup is closed
    $("#videoModal").on('hide.bs.modal', function () {
        player.stopVideo();
    });

</script>

答案 3 :(得分:-2)

我没有对此进行测试,但您可以使用Javascript并为播放器调用播放功能,甚至只需在页面加载时单击该元素。

答案 4 :(得分:-3)

无法做到。由于各种原因(包括但不限于数据使用),Apple不允许自动播放视频。

请参阅:

Youtube embedded video: autoplay feature not working in iphone