使用Youtube Iframe API创建的视频播放器停止使用Chrome v.85

问题描述

在将Youtube iframe API与最新的稳定版Chrome(版本85)结合使用时遇到问题。我知道一个月前一切正常,但是现在,即使完全遵循在YouTube iframe API文档中找到的最基本的示例:

https://developers.google.com/youtube/iframe_api_reference#Getting_Started

不再起作用。无法触发onReady和onStateChange事件,并且“ player”对象缺少其大部分功能,例如,player.playVideo()未定义。在使用它进行测试的任何其他浏览器上都不会出现该问题,仅当在测试时登录到我的Youtube帐户时,该问题也才会发生。

我怀疑问题的根源是来自YouTube的请求发送的Cookie,因为我在控制台的“发现问题”标签中收到此错误:

“通过指定其SameSite属性来指示是否在跨站点请求中发送cookie”

,而Chrome在85版中提到了这一点:

“拒绝不安全的SameSite =无cookie”

如果我从YouTube登出,播放器就可以正常工作,因为YouTube使用这些Cookie根据您的个人资料建议其他视频。

此刻我正在寻找一种解决方法,我唯一能找到的是,如果我手动创建iframe,而不是使用API​​,则可以将“ youtube-nocookie”而不是“ youtube”放在src中iframe,但是那样,我没有可以用来控制播放器的对象,例如,如果我必须创建一个自定义按钮来暂停/播放视频的话。我想主要是在YouTube上修复他们的API,但是现在有什么方法可以解决此问题吗?

下面是一个Codepen来说明问题,该代码取自上面链接的Youtube Iframe API文档:

https://codepen.io/Gabielovv/pen/VwadJvg?editors=1111

  // 2. This code loads the IFrame Player API code asynchronously.
  var tag = document.createElement('script');

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

  // 3. This function creates an <iframe> (and YouTube player)
  //    after the API code downloads.
  var player;
  function onYouTubeIframeAPIReady() {
    player = new YT.Player('player',{
      height: '390',width: '640',videoId: 'M7lc1UVf-VE',events: {
        'onReady': onPlayerReady,'onStateChange': onPlayerStateChange
      }
    });
  }

  // 4. The API will call this function when the video player is ready.
  function onPlayerReady(event) {
    console.log("onPlayerReady");
    event.target.playVideo();
  }

  // 5. The API calls this function when the player's state changes.
  //    The function indicates that when playing a video (state=1),//    the player should play for six seconds and then stop.
  var done = false;

  function onPlayerStateChange(event) {
    console.log("onPlayerStateChange");
    if (event.data == YT.PlayerState.PLAYING && !done) {
      setTimeout(stopVideo,6000);
      done = true;
    }
  }

  function stopVideo() {
    player.stopVideo();
  }

  function playYtVideo() {
    console.log("playYtVideo");
    player.playVideo();
  }

解决方法

从此Google Issue Tracker找到了解决方法。看来这是浏览器问题。

尝试那些:

希望如此?

,

只是测试了一下,现在看来一切正常,我还看到Google Issue Tracker上的人说了同样的话,所以我想这个问题已经解决,谢谢。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...