【问题标题】:How to set video ads in iframe youtube player API in javascript如何在 javascript 中的 iframe youtube player API 中设置视频广告
【发布时间】:2015-10-02 06:24:04
【问题描述】:

我在我的网站中使用 javascript Youtube Player API 来播放我的视频表单 youtube

为此我使用了以下代码:

<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',
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
      }

      // 4. The API will call this function when the video player is ready.
      function onPlayerReady(event) {
        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) {
        if (event.data == YT.PlayerState.PLAYING && !done) {
          setTimeout(stopVideo, 6000);
          done = true;
        }
      }
      function stopVideo() {
        player.stopVideo();
      }
    </script>
  </body>
</html>

问题是: 如何在此播放器中开始播放视频之前设置 youtube 视频广告 或者这种类型的设置在 iframe youtube player API 中不可用?

【问题讨论】:

    标签: javascript youtube-iframe-api


    【解决方案1】:

    iframe 中显示的内容由视频作者设置 - 由于 iframe 只是 youtube 页面的一个框架,您无法禁用其中的一部分。

    【讨论】:

    • 感谢@Lukas Valatka 当我在我的应用程序中使用 android youtube 播放器 API 播放此视频时,它会显示视频广告,但是当我在我的网站 javascript youtube 播放器 API 中打开相同的视频时,它只会显示横幅广告。
    • 没关系,javascript 代码不应干扰广告。尝试移除超时,让视频播放时间超过 6 秒。
    猜你喜欢
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 2021-07-31
    • 2013-11-24
    • 2014-12-19
    • 2014-01-05
    • 2015-09-07
    • 2012-10-28
    相关资源
    最近更新 更多