Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    191
    Thanks
    6
    Thanked 0 Times in 0 Posts

    YouTube Player Iframe API autoplay = 0 problem

    Hello,

    I'm wondering if anyone out there is familiar with the YouTube Iframe API? I'm trying to set autoplay to 0 so the video doesn't autostart and it doesn't seem to be working. Wondering if someone can take a look and see if I've got something wrong here? Seems like I must be missing something. The video plays fine otherwise. Thanks.

    Code:
      <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('video', {
              height: '280',
              width: '500',
            playerVars: { 'autoplay': 0, 'showinfo': 0, 'rel': 0 },
              videoId: 'MY-YouTube-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();
          }
          
          // 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, 0000);
              done = true;
            }
          }
          function stopVideo() {
            player.stopVideo();
          }
      </script>

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    I'm not familiar with YouTube API but the video autoplays probably because of this?
    Code:
         function onYouTubeIframeAPIReady() {
            player = new YT.Player('video', {
              height: '280',
              width: '500',
            playerVars: { 'autoplay': 0, 'showinfo': 0, 'rel': 0 },
              videoId: 'MY-YouTube-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();
          }
    Maybe event.target.playVideo() ignores the autoplay setting.

  • Users who have thanked glenngv for this post:

    wyclef (09-13-2013)

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    191
    Thanks
    6
    Thanked 0 Times in 0 Posts
    thanks. that helped a bit i think.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •