Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function initAllVideos() {
- var ytplayerList;
- function onPlayerReady(e) {
- var video_data = e.target.getVideoData(),
- label = video_data.video_id + ':' + video_data.title;
- e.target.ulabel = label;
- console.log(label + " is ready!");
- };
- function onPlayerError(e) {
- console.log('[onPlayerError]');
- };
- function onPlayerStateChange(e) {
- var label = e.target.ulabel;
- if (e["data"] == YT.PlayerState.PLAYING) {
- pauseOthersYoutubes(e.target);
- pauseHtmlVideo();
- console.log(22)
- }
- //track number of buffering and quality of video
- if (e["data"] == YT.PlayerState.BUFFERING) {
- e.target.uBufferingCount ? ++e.target.uBufferingCount : e.target.uBufferingCount = 1;
- if (YT.PlayerState.UNSTARTED == e.target.uLastPlayerState) {
- pauseOthersYoutubes(e.target);
- pauseHtmlVideo();
- console.log(33)
- }
- }
- //last action keep stage in uLastPlayerState
- if (e.data != e.target.uLastPlayerState) {
- e.target.uLastPlayerState = e.data;
- }
- };
- function initYoutubePlayers() {
- ytplayerList = null; //reset
- ytplayerList = []; //create new array to hold youtube player
- for (var e = document.querySelectorAll(".js-lp-video-youtube"), x = e.length; x--;) {
- if (/youtube.com\/embed/.test(e[x].src)) {
- ytplayerList.push(initYoutubePlayer(e[x]));
- }
- }
- console.log(ytplayerList)
- };
- function pauseOthersYoutubes(currentPlayer) {
- console.log(ytplayerList)
- if (!currentPlayer) return;
- for (var i = ytplayerList.length; i--;) {
- if (ytplayerList[i] && (ytplayerList[i] != currentPlayer)) {
- console.log(ytplayerList[i])
- ytplayerList[i].pauseVideo();
- }
- }
- };
- //init a youtube iframe
- function initYoutubePlayer(ytiframe) {
- var ytp = new YT.Player(ytiframe, {
- events: {
- onStateChange: onPlayerStateChange,
- onError: onPlayerError,
- onReady: onPlayerReady
- }
- });
- ytiframe.ytp = ytp;
- return ytp;
- };
- function pauseOnHtmlVideoPlaying() {
- let $video = $('.js_html_video');
- $video.each(function () {
- $(this).on('playing', function () {
- $video.removeClass('js_playing_video')
- $(this).addClass('js_playing_video');
- $video.each(function () {
- if (!$(this).hasClass('js_playing_video')) {
- $(this)[0].pause();
- };
- });
- pauseOthersYoutubes($('.js-lp-video-youtube'))
- });
- });
- };
- function pauseHtmlVideo() {
- let $video = $('.js_html_video');
- $video.each(function () {
- $(this)[0].pause();
- });
- };
- var yt_int;
- yt_int = setInterval(function(){
- if(typeof YT === "object"){
- clearInterval(yt_int);
- setTimeout(function(){
- initYoutubePlayers();
- }, 1500);
- }
- },500);
- setTimeout(function(){
- pauseOnHtmlVideoPlaying();
- }, 500);
- };
- window.addEventListener('DOMContentLoaded', () => {
- initAllVideos();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement