0

I was trying to recreate the multiple iframes example detailed here for a slideshow. It works flawlessly in all browsers, including desktop safari, except on iphone and ipad.

http://codepen.io/lakshminp/pen/mepIB

Js:

var tag = document.createElement('script');

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

var players = {}

var YT_ready = (function() {
    var onReady_funcs = [],
        api_isReady = false;
    return function(func, b_before) {
        if (func === true) {
            api_isReady = true;
            for (var i = 0; i < onReady_funcs.length; i++) {
                // Removes the first func from the array, and execute func                                                                                                                     
                onReady_funcs.shift()();
            }
    }
        else if (typeof func == "function") {
            if (api_isReady) func();
            else onReady_funcs[b_before ? "unshift" : "push"](func);
        }
    }
})();


function onYouTubeIframeAPIReady() {
  jQuery(window).bind("load", function() {
    YT_ready(true);
  });
}

YT_ready(function() {
        var identifier = "v9d09JLBVRc";
            players[identifier] = new YT.Player(identifier, {
                events: {
                  'onReady': onPlayerReady,
                    "onStateChange": createYTEvent(identifier)
                }
            });
});

function onPlayerReady(event) {
  event.target.playVideo();
}

// Returns a function to enable multiple events                                                                                                                                                
function createYTEvent(identifier) {
    var player = players[identifier]; // Get cached player instance                                                                                                                            
    return function (event) {
      console.log(event.data);
    }
}

HTML:

<div id="yt-container">
  <iframe width="640" height="480" id="v9d09JLBVRc" class="media-youtube-player" src="//www.youtube.com/embed/v9d09JLBVRc?enablejsapi=1&autoplay=0" frameborder="0" allowfullscreen></iframe>
</div>
Community
  • 1
  • 1
Badri
  • 2,212
  • 3
  • 25
  • 26

1 Answers1

0

According to this

functions and parameters such as autoplay, playVideo(), loadVideoById() won't work in all mobile environments

and in your YTReady function, you load dynamically a video and for the moment that's not possible on iOS.

You can also try the YouTubeDemoPage and try to load another id and you will see that it can't be done.

You should also check this.

Community
  • 1
  • 1
raduanastase
  • 143
  • 1
  • 8