【问题标题】:Javascript play youtube API (+queue videos)Javascript 播放 youtube API(+队列视频)
【发布时间】:2016-12-18 23:34:24
【问题描述】:

基本上,我已经设法创建了一个网站来使用 youtube video api 搜索视频。

我现在想添加一个 youtubevideo 播放器(iframe?),并且可能能够从文本输入中对视频(url)进行排队。

我的下一个问题,我在互联网上找到了使用 iframe 所需的代码, 但是脚本都在html页面中,我想知道是否可以将代码放在我的JS页面中..我试过了,但不起作用......我认为将&之间的语法放在我的js中文件就可以了...

有人可以解释我如何在将代码放在我的 js 文件中时使其工作吗? 我知道这可能是一个愚蠢的问题......

非常感谢!

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

【问题讨论】:

  • 我认为在我链接的 js 文件中的脚本之间放置文本会很简单,但它没有......

标签: javascript api iframe youtube


【解决方案1】:

我尝试了以下方式;

in html: 
    <script src="js/app.js"></script>
in js;
      // 2. This code loads the IFrame Player API code asynchronously.
      var tag = document.createElement('script');


..... etc
      }

【讨论】:

    猜你喜欢
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2017-06-25
    • 2017-02-14
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    相关资源
    最近更新 更多