0

I'm trying to create a button in my website and when you press that button youtube player should start playing.

Now to do that i am using Youtube player API and playVideo method and everything seemed to be working just fine until i tried testing it on mobile devices, for example iphone.

Player seems to get bugged when playVideo method is called so my best guess is that its fault on google end but i can't seem to find any fix for that.

I made jsFiddle

Also demo website to test it on mobile devices: link

And here is the code

<div id="ytplayer"></div>
<a id="start" href="#">start</a>

<script>
  var tag = document.createElement('script');
  tag.src = "https://www.youtube.com/player_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  var player;
  function onYouTubePlayerAPIReady() {
    player = new YT.Player('ytplayer', {
      height: '390',
      width: '640',
      videoId: 'U6oKcAXiVlo',
      playerVars: { 'autoplay': 0, 'controls': 0, 'showinfo': 0 },
    });
  }

  document.getElementById("start").addEventListener("click", function () {
  player.playVideo();
  });
</script>
Linas
  • 4,380
  • 17
  • 69
  • 117
  • 1
    If I'm not mistaken, iPhone (browser or OS) prevents automatic playing of media contents. – barak manos Oct 22 '14 at 11:24
  • This answer might help Click [Autoplay on iOS](http://stackoverflow.com/questions/12496144/can-you-autoplay-html5-videos-on-the-ipad) – Brian Stacks Oct 22 '14 at 11:35
  • @barakmanos But i'm not trying to make automatic playing. Video should only play when click event gets triggered. – Linas Oct 22 '14 at 11:45
  • It's not just autoplay; most mobile browsers don't support scripted playback of video, either, as it's too easy to eat up users' data caps. You'll have to rely on the play button on the face of the player rather than create your own. Note that once the player itself has started playback, then your custom button might start working (depending on the browser). – jlmcdonald Oct 22 '14 at 22:19

0 Answers0