Advertisement
Guest User

Youtuber Player APi

a guest
Dec 11th, 2017
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.57 KB | None | 0 0
  1. <html>
  2.   <body>
  3.     <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
  4.     <div id="player"></div>
  5.  
  6.     <script>
  7.       // 2. This code loads the IFrame Player API code asynchronously.
  8.       var tag = document.createElement('script');
  9.  
  10.       tag.src = "https://www.youtube.com/iframe_api";
  11.       var firstScriptTag = document.getElementsByTagName('script')[0];
  12.       firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  13.  
  14.       // 3. This function creates an <iframe> (and YouTube player)
  15.       //    after the API code downloads.
  16.       var player;
  17.       function onYouTubeIframeAPIReady() {
  18.         player = new YT.Player('player', {
  19.           height: '360',
  20.           width: '640',
  21.           videoId: 'M7lc1UVf-VE',
  22.           events: {
  23.             'onReady': onPlayerReady,
  24.             'onStateChange': onPlayerStateChange
  25.           }
  26.         });
  27.       }
  28.  
  29.       // 4. The API will call this function when the video player is ready.
  30.       function onPlayerReady(event) {
  31.         event.target.playVideo();
  32.       }
  33.  
  34.       // 5. The API calls this function when the player's state changes.
  35.       //    The function indicates that when playing a video (state=1),
  36.       //    the player should play for six seconds and then stop.
  37.       var done = false;
  38.       function onPlayerStateChange(event) {
  39.         if (event.data == YT.PlayerState.PLAYING && !done) {
  40.          setTimeout(stopVideo, 6000);
  41.           done = true;
  42.         }
  43.       }
  44.       function stopVideo() {
  45.         player.stopVideo();
  46.       }
  47.     </script>
  48.   </body>
  49. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement