Advertisement
irkopkopen

yt.js

Jan 25th, 2022
974
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var tag = document.createElement('script');
  2. tag.src = "https://www.youtube.com/iframe_api";
  3. var firstScriptTag = document.getElementsByTagName('script')[0];
  4. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  5. // 3. This function creates an <iframe> (and YouTube player)
  6. //    after the API code downloads.
  7. var player;
  8. function onYouTubeIframeAPIReady() {
  9.   player = new YT.Player('player', {
  10.     height: '390',
  11.     width: '640',
  12.     videoId: 'Mc4jAiJameA',
  13.     events: {
  14.       'onReady': onPlayerReady,
  15.       'onStateChange': onPlayerStateChange
  16.     }
  17.   });
  18. }
  19. // 4. The API will call this function when the video player is ready.
  20. function onPlayerReady(event) {
  21.   event.target.playVideo();
  22.   player.mute();
  23. }
  24. // 5. The API calls this function when the player's state changes.
  25. //    The function indicates that when playing a video (state=1),
  26. //    the player should play for six seconds and then stop.
  27. var done = false;
  28. function onPlayerStateChange(event) {
  29.   if (event.data == YT.PlayerState.PLAYING && !done) {
  30.     setTimeout(stopVideo, 6000);
  31.     done = true;
  32.   }
  33. }
  34. function stopVideo() {
  35.   player.stopVideo();
  36. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement