YouTube Iframe API:不在IOS上自动播放

时间:2018-02-08 01:07:22

标签: html iframe youtube

我正在使用YouTube iFrame API嵌入视频,该视频会在加载后立即播放。除了移动IOS外,视频自动加载到处。我怎样才能让它自动播放?我根据另一个帖子的评论添加了event.target.mute()代码,但没有运气。您可以忽略间隔中的代码 - 这只是为了帮助视频循环产生淡入淡出效果。

iframe API代码:

  <div id="player" class="hero__vid"></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: '390',
          width: '640',
          videoId: MyVidID,
          playerVars: {
            controls: 0,
            showinfo:0,
            rel:0,
          },
          events: {
            'onReady': onPlayerReady,
          }
        });
      }

      // 4. The API will call this function when the video player is ready.
      function onPlayerReady(event) {
        event.target.mute();
        event.target.playVideo();

        var interval_is_stopped = false;
        setInterval(function (){
          var current_time = event.target.getCurrentTime();

          if (current_time > 14.9 && !interval_is_stopped) {
            interval_is_stopped = true;
            jQuery('#player').fadeTo(400, 0.7, function(){
              player.seekTo(0);
              jQuery(this).fadeTo(400, 1, function(){
                interval_is_stopped = false;
              });
            });
          }
        }, 10);

      }

    </script>

4 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我发现了如何做到这一点,Thanks to this answer。 IOS 10+实际上允许现在这样做,但遗憾的是它仅适用于HTML5视频标签,因此YouTube的iframe无法工作,这真是一种耻辱!

这是我使用的代码

&#13;
&#13;
<video playsinline preload="auto" loop muted autoplay>
  <source src="vid.mp4'" type="video/mp4">
  Sadly, your browser does not support the video tag X_x
  </video>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

'playsinline': 1用于iOS,它将起作用

答案 3 :(得分:0)

如果要开发iOS应用程序,请使用此功能。

要使youtube播放器自动播放,您需要使用以下配置初始化WKWebView

let config = WKWebViewConfiguration()
config.allowsInlineMediaPlayback = true

// this will not require any gesture for triggering playback
config.mediaTypesRequiringUserActionForPlayback = []

// NOTE: WKWebView should be initialized in code as need to set it up 
// with custom configuration. 
// This is not possible through Interface Builder.
let webView = WKWebView(frame: CGRect.zero, configuration: config)

此外,请确保您的JavaScript代码中包含以下代码。取自YouTube Player API reference


      // ...

      // 3. This function creates an <iframe> (and YouTube player)
      //    after the API code downloads.
      var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          height: '390',
          width: '640',
          videoId: 'M7lc1UVf-VE',
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
      }

      // 4. The API will call this function when the video player is ready.
      function onPlayerReady(event) {
        event.target.playVideo();
      }

      // ...