YouTube API 目标(多个)现有 iframe

发布于 2024-12-28 15:10:15 字数 416 浏览 1 评论 0 原文

我试图了解如何使用 YouTube API 定位现有的 iframe(即无需使用脚本构建 iframe)。

与往常一样,Google 没有提供足够的 API 示例,但解释说这是可能的,此处 http://code.google .com/apis/youtube/iframe_api_reference.html

这是我正在尝试执行的示例 - 缩略图下方的视频应该播放。我快到了,但只播放第一个视频...

http://jsfiddle.net/SparrwHawk/KtbYR/2/< /a>

I'm trying to understand how to target an existing iframe using the YouTube API (i.e. without constructing an iframe with the script).

As usual, Google does not give enough API examples, but explains that it IS possible, here http://code.google.com/apis/youtube/iframe_api_reference.html

Here is an example of what I'm trying to do - the video underneath the thumbnail should play. I am almost there, but only the first video plays...

http://jsfiddle.net/SparrwHawk/KtbYR/2/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音盲 2025-01-04 15:10:15

TL;DR:演示:http://jsfiddle.net/KtbYR/5/

YT_ready getFrameIDonYouTubePlayerAPIReady这个答案。这两种方法都可以在没有任何预加载库的情况下实现。在我之前的回答中,我展示了一种为单帧实现该功能的方法。

在这个答案中,我重点关注多个框架。

HTML 示例代码(重要标签和属性均大写,

<div>
  <img class='thumb' src='http://i2.cdnds.net/11/34/odd_alan_partridge_bio_cover.jpg'>
  <IFRAME ID="frame1" SRC="http://www.youtube.com/embed/u1zgFlCw8Aw?enablejsapi=1" width="640" height="390" frameborder="0"></IFRAME>
</div>
<div>
  <img class='thumb' src='http://i2.cdnds.net/11/34/odd_alan_partridge_bio_cover.jpg'>
  <IFRAME ID="frame2" SRC="http://www.youtube.com/embed/u1zgFlCw8Aw?enablejsapi=1" width="640" height="390" frameborder="0"></IFRAME>
</div>

JavaScript 代码 (YT_ready, getFrameIDonYouTubePlayerAPIReadyYouTube Frame API 脚本加载器定义为 此处

var players = {}; //Define a player storage object, to expose methods,
                  //  without having to create a new class instance again.
YT_ready(function() {
    $(".thumb + iframe[id]").each(function() {
        var identifier = this.id;
        var frameID = getFrameID(identifier);
        if (frameID) { //If the frame exists
            players[frameID] = new YT.Player(frameID, {
                events: {
                    "onReady": createYTEvent(frameID, identifier)
                }
            });
        }
    });
});

// Returns a function to enable multiple events
function createYTEvent(frameID, identifier) {
    return function (event) {
        var player = players[frameID]; // Set player reference
        var the_div = $('#'+identifier).parent();
        the_div.children('.thumb').click(function() {
            var $this = $(this);
            $this.fadeOut().next().addClass('play');
            if ($this.next().hasClass('play')) {
                player.playVideo();
            }
        });
    }
}

在我之前的回答中,我绑定了onStateChange 事件。在此示例中,我使用了 onReady 事件,因为您只想在初始化时调用这些函数一次。

此示例的工作原理如下:

  • 以下方法在此中定义回答

    1. YouTube Frame API 是从 http://youtube.com/player_api.
    2. 此外部脚本加载完成后,将调用 onYoutubePlayerAPIReady,进而激活使用 YT_ready 定义的所有函数
  • 此处显示以下方法的声明,但使用这个实现回答。基于示例的说明:

    1. 循环遍历每个

请确保您检查此答案< /a>,因为这个答案的核心功能就是基于此。

其他 YouTube Frame API 答案。在这些答案中,我展示了 YouTube Frame/JavaScript API 的各种实现。

TL;DR: DEMO: http://jsfiddle.net/KtbYR/5/

YT_ready, getFrameID and onYouTubePlayerAPIReady are functions as defined in this answer. Both methods can be implemented without any preloaded library. In my previous answer, I showed a method to implement the feature for a single frame.

In this answer, I focus on multiple frames.

HTML example code (important tags and attributes are capitalized, <iframe src id>):

<div>
  <img class='thumb' src='http://i2.cdnds.net/11/34/odd_alan_partridge_bio_cover.jpg'>
  <IFRAME ID="frame1" SRC="http://www.youtube.com/embed/u1zgFlCw8Aw?enablejsapi=1" width="640" height="390" frameborder="0"></IFRAME>
</div>
<div>
  <img class='thumb' src='http://i2.cdnds.net/11/34/odd_alan_partridge_bio_cover.jpg'>
  <IFRAME ID="frame2" SRC="http://www.youtube.com/embed/u1zgFlCw8Aw?enablejsapi=1" width="640" height="390" frameborder="0"></IFRAME>
</div>

JavaScript code (YT_ready, getFrameID, onYouTubePlayerAPIReady and the YouTube Frame API script loader are defined here)

var players = {}; //Define a player storage object, to expose methods,
                  //  without having to create a new class instance again.
YT_ready(function() {
    $(".thumb + iframe[id]").each(function() {
        var identifier = this.id;
        var frameID = getFrameID(identifier);
        if (frameID) { //If the frame exists
            players[frameID] = new YT.Player(frameID, {
                events: {
                    "onReady": createYTEvent(frameID, identifier)
                }
            });
        }
    });
});

// Returns a function to enable multiple events
function createYTEvent(frameID, identifier) {
    return function (event) {
        var player = players[frameID]; // Set player reference
        var the_div = $('#'+identifier).parent();
        the_div.children('.thumb').click(function() {
            var $this = $(this);
            $this.fadeOut().next().addClass('play');
            if ($this.next().hasClass('play')) {
                player.playVideo();
            }
        });
    }
}

In my previous answer, I bound the onStateChange event. In this example, I used the onReady event, because you want to call the functions only once, at initialization.

This example works as follows:

  • The following methods are defined in this answer.

    1. The YouTube Frame API is loaded from http://youtube.com/player_api.
    2. When this external script has finished loading, onYoutubePlayerAPIReady is called, which in his turn activates all functions as defined using YT_ready
  • The declaration of the following methods are shown here, but implemented using this answer. Explanation based on the example:

    1. Loops through each <iframe id> object, which is placed right after <.. class="thumb">.
    2. At each frame element, the id is retrieved, and stored in the identifier variable.
    3. The internal ID of the iframe is retrieved through getFrameID. This ensures that the <iframe> is properly formatted for the API. In this example code, the returned ID is equal to identifier, because I have already attached an ID to the <iframe>.
    4. When the <iframe> exists, and a valid YouTube video, a new player instance is created, and the reference is stored in the players object, accessible by key frameID.
    5. At the creation of the player instance, a **onReady* event is defined. This method will be invoked when the API is fully initialized for the frame.
    6. createYTEvent
      This method returns a dynamically created function, which adds functionality for separate players. The most relevant parts of the code are:

      function createYTEvent(frameID, identifier) {
          return function (event) {
              var player = players[frameID]; // Set player reference
              ...
                      player.playVideo();
          }
      }
      
      • frameID is the ID of the frame, used to enable the YouTube Frame API.
      • identifier is the ID as defined in YT_ready, not necessarily an <iframe> element. getFrameID will attempt to find the closest matching frame for a given id. That is, it returns the ID of a given <iframe> element, or: If the given element is not an <iframe>, the function looks for a child which is a <iframe>, and returns the ID of this frame. If the frame does not exists, the function will postfix the given ID by -frame.
      • players[playerID]` refers to the initialized player instance.

Make sure that you also check this answer, because the core functionality of this answer is based on that.

Other YouTube Frame API answers. In these answers, I showed various implementations of the YouTube Frame/JavaScript API.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文