为许多视频初始化时如何获取 Plyr 当前实例

How to get Plyr current instance when initialized for many videos

我在我的网页中使用 Plyr 来播放其中嵌入的 Youtube。在我的页面中,我有大约 10 个 videos/embeds,我当前的设置如下:

HTML

<div class="plyr" data-plyr-provider="youtube" data-plyr-embed-id="videoID1"></div>
<div class="plyr" data-plyr-provider="youtube" data-plyr-embed-id="videoID2"></div>
<div class="plyr" data-plyr-provider="youtube" data-plyr-embed-id="videoID3"></div>
<div class="plyr" data-plyr-provider="youtube" data-plyr-embed-id="videoID4"></div>
<div class="plyr" data-plyr-provider="youtube" data-plyr-embed-id="videoID5"></div>
<div class="plyr" data-plyr-provider="youtube" data-plyr-embed-id="videoID6"></div>
<div class="plyr" data-plyr-provider="youtube" data-plyr-embed-id="videoID7"></div>
<div class="plyr" data-plyr-provider="youtube" data-plyr-embed-id="videoID8"></div>
<div class="plyr" data-plyr-provider="youtube" data-plyr-embed-id="videoID9"></div>
<div class="plyr" data-plyr-provider="youtube" data-plyr-embed-id="videoID10"></div>

JS

const players = Plyr.setup('.plyr', options);

现在,如果我在页面上播放视频 #3,我如何才能获得该实例,以便我可以控制播放器并收听该实例事件,以便我可以用这些执行其他操作。

文档仅在实例化一个播放器时讨论单实例

const player = new Plyr('#player', options);

但我一次实例化了多个...

你应该做的是像这样将你的变量添加到 data-plyr-config

<div class="plyr" data-plyr-provider="youtube" data-plyr-embed-id="videoID10" data-plyr-config='{ "the_id": "YOUR ID" }'></div>

然后在你的Js代码中你可以获得变量

const plyr = Plyr.setup('. plyr', config);
    plyr.forEach(function (instance) {
        instance.on('playing', function () {
            console.log(instance.config.the_id);
        });
    });
  • 其中 'the_id' 是您刚刚创建的密钥
  • 注意:键名可以是任意的