【问题标题】:Instead of played automatically, Youtube video do nothing or show suggested videosYoutube 视频不会自动播放,而是什么都不做或显示推荐的视频
【发布时间】:2019-05-26 11:42:55
【问题描述】:

我想做的是在某个时刻播放嵌入的 Youtube 视频。为此,在给定时间我运行了 javascript 代码

document.getElementById("video").src="https://www.youtube.com/embed/...?autoplay=1&start=212";

视频是我的 iframe。 现在的问题 - 在一个浏览器 (IE) 中它可以正常工作,视频会在给定的时刻自动开始。相反,在 Chrome 中它不会启动,但会显示建议的视频 - 参见图片。它显示建议不要播放视频后,而是而不是播放。在移动浏览器(Samsung 和 Ghostery)中,它只会出现但不会播放。 这种行为的原因可能是什么?可能与我的 gmail(以及 youtube)帐户设置或浏览器设置有关吗?

【问题讨论】:

  • 我的代码很简单。首先,我放了一个带有暂停视频的 iframe:<iframe width="560" height="315" id="video" src="https://www.youtube.com/embed/M7lc1UVf-VE?&start=212" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="allowfullscreen"></iframe>
  • 在给定时刻播放视频后(在这种情况下为 59 秒)<script> var now = new Date(); if (now.getSeconds()==59){ document.getElementById("viedao").src= "https://www.youtube.com/embed/M7lc1UVf-VE?autoplay=1&start=212"; } else { setTimeout(function(){ document.getElementById("viedo").src= "https://www.youtube.com/embed/M7lc1UVf-VE?autoplay=1&start=212"; }, 1000*(59-now.getSeconds())); } </script>

标签: javascript video browser cross-browser embedded-video


【解决方案1】:

请添加您的代码和屏幕截图,以便更好地理解该问题。另外,我认为您应该查看Youtube iFrame Player API。它将为您提供您正在寻找的功能。试试这个,它适用于 Chrome、IE、Firefox。不过,也许您需要检查移动选项。

  <!DOCTYPE html>
    <html>
      <body>
        <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
        <div id="player"></div>

        <script>
          // 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',
              playerVars: {
                'autoplay': 1,
                'start' : 212
              },
              events: {
                'onReady': onPlayerReady
              }
            });
          }

          // 4. The API will call this function when the video player is ready.
          function onPlayerReady(event) {
            event.target.playVideo();
          }
        </script>
      </body>
    </html>

【讨论】:

  • 谢谢,但您的代码结果完全相同。在 Chrome 中,它显示已暂停的视频并在其上推荐视频。所以它不会自动播放。 我添加了屏幕截图,但它们不在这里 - 可能是因为我还不允许添加图像。
  • 我在 Chrome 中而不是自动播放视频的屏幕截图 i.stack.imgur.com/OPQ9a.jpg
  • 哦!这很奇怪,你能看到你的控制台日志,看看当视频在 chrome 中暂停时你得到了什么错误(如果有的话)吗?我认为这将有助于缩小问题范围。
猜你喜欢
  • 1970-01-01
  • 2012-03-11
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
  • 2018-04-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多