【问题标题】:Open the website when a youtube video is loaded加载 youtube 视频时打开网站
【发布时间】:2019-12-27 22:20:56
【问题描述】:

我在首页的背景中嵌入了这个自动播放的 youtube 视频,我想要做的是保持加载页面继续运行,直到视频完全加载,以便人们在他们离开时可以看到它正在播放在这里的问题是即使视频仍在加载中,加载页面也会打开。

我已经为加载页面设置了加载功能,假设它在页面加载后执行操作?有没有办法用这个加载功能来检测 youtube 视频是否已完全加载?

这是youtube的js代码

if(!navigator.userAgent.match(/(iPhone|iPad|iPod|Android)/)){

  var tag = document.createElement('script');

  tag.src = "https://www.youtube.com/iframe_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  var player;
  function onYouTubeIframeAPIReady() {
    player = new YT.Player('top_video', {
      width: '100%',
      videoId: 'ZHe75UFH6uE',
      playerVars : {
            'loop' : 1,
            'autoplay' : 1,
            'mute' : 1,
            'rel' : 0,
            'showinfo' : 0,
            'controls' : 0,
            'modestbranding' : 1,
            'iv_load_policy' : 3,
            'cc_load_policy' : 1,
            'origin': location.protocol + '//' + location.hostname + "/"
        },
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange
      }
    });
  }

  function onPlayerReady(event) {
    event.target.playVideo();
  var interval_is_stopped = false;
        setInterval(function (){
          var current_time = event.target.getCurrentTime();

          if (current_time > 81.9 && !interval_is_stopped) {
            interval_is_stopped = true;
            jQuery('#top_video').fadeTo(400, 0.7, function(){
              player.seekTo(0);
              jQuery(this).fadeTo(400, 1, function(){
                interval_is_stopped = false;
              });
            });
          }
        }, 10);
}

 var done = false;
      function onPlayerStateChange(event) {
        if (event.data==YT.PlayerState.CUED && !done) {
          done = true;
        }
      }

  $('#pauseplay').on('click', function(){
    var isMuted = player.isMuted();
    if( $(this).hasClass('sound_off_active') && isMuted ){
      $(this).removeClass('sound_off_active');
      $(this).addClass('sound_on_active');
      player.unMute();
    } else {
      $(this).addClass('sound_off_active');
      $(this).removeClass('sound_on_active');
      player.mute();
    }

  });


$(function () {
    $('a.disabled').on("click", function (e) {
        e.preventDefault();
    });
});

}

这里是加载js

var $delayTime = 3000;

$(window).on('load', function(){

    var $loadingAnim = $('.loadingAnim'),
        $body = $('body');

    setTimeout( function(){

        $body.addClass('loaded');

        $loadingAnim.find('.loadingAnim_line').on('transitionend', function( e ){
            $(this).parent().remove();
        });

    }, $delayTime );
});

这是我的网站

https://www.run-journey.com/

我需要在这个网站上显示一个视频

https://re-creation.co.jp/

【问题讨论】:

  • 将事件'onStateChange': onPlayerStateChange 添加到您的设置中,并在触发时将stateChanged 等全局变量设置为true 并相应地加载页面。
  • 感谢您的评论。我像上面一样编辑了代码,但我不知道如何以您解释的方式加载页面。我是否必须设置条件以查看 done 变量是否为真并用它包装整个加载 js 部分?我知道 done 变量不是全局变量,所以我不能用它来加载 js..对不起,我仍然是 javascript 的初学者。
  • 关闭,请参阅this answer。最终,您将不得不使用 css 和 js 来相应地显示或隐藏元素。
  • 不幸的是,您给我的答案完全无关紧要。视频结束时,我试图不对图像做任何事情。我只需要知道是否可以检测到视频是否已加载并在打开加载页面后立即播放视频。我不知道如何解释这一点,所以请再次查看我的网站,看看我的意思。

标签: javascript jquery youtube youtube-api load


【解决方案1】:

我会放弃设置的超时时间,让动画一直运行,直到视频加载(提示),类似于:

function onPlayerStateChange(event) {    
    if(event.data === 5) {          
        hideAnimation();
    }
}

function hideAnimation() {
    $('.loadingAnim').find('.loadingAnim_line').on('transitionend', function (e) {
        $(this).parent().remove();
    });
}

更多关于玩家状态的信息here

【讨论】:

  • 我按照您上面解释的方式解决了这个问题。非常感谢。
猜你喜欢
  • 2017-10-24
  • 1970-01-01
  • 2018-04-16
  • 2018-09-02
  • 2021-03-25
  • 1970-01-01
  • 2017-06-18
  • 1970-01-01
  • 2015-10-21
相关资源
最近更新 更多