Advertisement
Igor150195

yt_for_lpc4

Mar 1st, 2023
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. function initAllVideos() {
  2. var ytplayerList;
  3.  
  4.  
  5. function onPlayerReady(e) {
  6. var video_data = e.target.getVideoData(),
  7. label = video_data.video_id + ':' + video_data.title;
  8. e.target.ulabel = label;
  9.  
  10. console.log(label + " is ready!");
  11. };
  12.  
  13. function onPlayerError(e) {
  14. console.log('[onPlayerError]');
  15. };
  16.  
  17. function onPlayerStateChange(e) {
  18. var label = e.target.ulabel;
  19. if (e["data"] == YT.PlayerState.PLAYING) {
  20. pauseOthersYoutubes(e.target);
  21. pauseHtmlVideo();
  22.  
  23. console.log(22)
  24. }
  25. //track number of buffering and quality of video
  26. if (e["data"] == YT.PlayerState.BUFFERING) {
  27. e.target.uBufferingCount ? ++e.target.uBufferingCount : e.target.uBufferingCount = 1;
  28.  
  29. if (YT.PlayerState.UNSTARTED == e.target.uLastPlayerState) {
  30. pauseOthersYoutubes(e.target);
  31. pauseHtmlVideo();
  32.  
  33. console.log(33)
  34. }
  35. }
  36. //last action keep stage in uLastPlayerState
  37. if (e.data != e.target.uLastPlayerState) {
  38. e.target.uLastPlayerState = e.data;
  39. }
  40. };
  41.  
  42. function initYoutubePlayers() {
  43. ytplayerList = null; //reset
  44. ytplayerList = []; //create new array to hold youtube player
  45.  
  46. for (var e = document.querySelectorAll(".js-lp-video-youtube"), x = e.length; x--;) {
  47. if (/youtube.com\/embed/.test(e[x].src)) {
  48. ytplayerList.push(initYoutubePlayer(e[x]));
  49. }
  50. }
  51.  
  52. console.log(ytplayerList)
  53. };
  54.  
  55. function pauseOthersYoutubes(currentPlayer) {
  56. console.log(ytplayerList)
  57.  
  58. if (!currentPlayer) return;
  59. for (var i = ytplayerList.length; i--;) {
  60. if (ytplayerList[i] && (ytplayerList[i] != currentPlayer)) {
  61. console.log(ytplayerList[i])
  62. ytplayerList[i].pauseVideo();
  63. }
  64. }
  65. };
  66. //init a youtube iframe
  67. function initYoutubePlayer(ytiframe) {
  68. var ytp = new YT.Player(ytiframe, {
  69. events: {
  70. onStateChange: onPlayerStateChange,
  71. onError: onPlayerError,
  72. onReady: onPlayerReady
  73. }
  74. });
  75. ytiframe.ytp = ytp;
  76. return ytp;
  77. };
  78.  
  79. function pauseOnHtmlVideoPlaying() {
  80. let $video = $('.js_html_video');
  81.  
  82. $video.each(function () {
  83. $(this).on('playing', function () {
  84. $video.removeClass('js_playing_video')
  85. $(this).addClass('js_playing_video');
  86.  
  87. $video.each(function () {
  88. if (!$(this).hasClass('js_playing_video')) {
  89. $(this)[0].pause();
  90. };
  91. });
  92.  
  93. pauseOthersYoutubes($('.js-lp-video-youtube'))
  94. });
  95. });
  96. };
  97.  
  98. function pauseHtmlVideo() {
  99. let $video = $('.js_html_video');
  100.  
  101. $video.each(function () {
  102. $(this)[0].pause();
  103. });
  104. };
  105.  
  106.  
  107.  
  108. var yt_int;
  109.  
  110. yt_int = setInterval(function(){
  111. if(typeof YT === "object"){
  112. clearInterval(yt_int);
  113. setTimeout(function(){
  114. initYoutubePlayers();
  115. }, 1500);
  116. }
  117. },500);
  118.  
  119. setTimeout(function(){
  120. pauseOnHtmlVideoPlaying();
  121. }, 500);
  122. };
  123.  
  124.  
  125.  
  126. window.addEventListener('DOMContentLoaded', () => {
  127. initAllVideos();
  128. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement