如何在javascript中在iframe youtube播放器API中设置视频广告

时间:2022-05-28 14:47:25

I am using javascript Youtube Player API in my website to play my video form youtube

我在我的网站上使用javascript Youtube Player API来播放我的视频形式youtube

For that i used following code:

为此,我使用以下代码:

<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>

Problem is that: How can i set youtube video ads before starting video in this player or this type of setting is not available in iframe youtube player API?

问题是:如何在此播放器中启动视频之前设置YouTube视频广告,或者iframe youtube播放器API中没有此类设置?

1 个解决方案

#1


0  

Whatever shows up in the iframe is set by the author of the video - as the iframe is merely a frame of youtube page, you cannot disable what is or not is a part of it.

iframe中显示的内容是由视频的作者设置的 - 因为iframe只是youtube页面的一个框架,你不能禁用它的一部分是什么。

#1


0  

Whatever shows up in the iframe is set by the author of the video - as the iframe is merely a frame of youtube page, you cannot disable what is or not is a part of it.

iframe中显示的内容是由视频的作者设置的 - 因为iframe只是youtube页面的一个框架,你不能禁用它的一部分是什么。