onYouTubeIframeAPIReady只调用一次,但页面上需要多个视频

and*_*yg1 4 youtube-api

我正在使用服务器端方法来放入带有播放列表和功能按钮的YouTube视频(想想一个网页小部件,无论如何都可以在页面上调用,并且可能在页面上多次调用).

我正在使用IFrame API.我可以通过在onYouTubeIframeAPIReady()方法中创建一个新的YT.Player实例来获取单个视频.这对我来说很有意义 - 等待加载库.但是当我想在一个页面上有多个视频播放器时,我不知道如何触发第二个,第三个,第四个等的启动.

我无法定义另一个onYouTubeIframeAPIReady()方法,因为它将覆盖第一个.如何在页面中添加更多玩家?在初始方法被解雇后,没有办法创建更多视频,这似乎很奇怪......

有关上述方法的文档,请访问:https: //developers.google.com/youtube/iframe_api_reference#Loading_a_Video_Player

提前致谢.

编辑:(在Miha Lampret的第一个回答之后澄清)

我无法在onYouTubeIframeAPIReady()方法中声明其他播放器,因为此代码是通过称为"小部件"的服务器端引入的.所以而不是:

function onYouTubeIframeAPIReady() {
    ytplayer1 = new YT.Player('player-youtube-1', {
        width: '640',
        height: '480',
        videoId: 'M7lc1UVf-VE'
    });

    ytplayer2 = new YT.Player('player-youtube-2', {
        width: '640',
        height: '480',
        videoId: 'smEqnnklfYs'
    });
}
Run Code Online (Sandbox Code Playgroud)

我的代码相当于:

function onYouTubeIframeAPIReady() {
    ytplayer1 = new YT.Player('player-youtube-1', {
        width: '640',
        height: '480',
        videoId: 'M7lc1UVf-VE'
    });
}
function onYouTubeIframeAPIReady() {
    ytplayer2 = new YT.Player('player-youtube-2', {
        width: '640',
        height: '480',
        videoId: 'smEqnnklfYs'
    });
}
Run Code Online (Sandbox Code Playgroud)

onYouTubeIframeAPIReady()只执行一次.我需要检查的是是否已经执行过一次.

Mih*_*ret 8

onYouTubeIframeAPIReady()在准备好使用YouTube API之后执行,即在API的Javascript文件http://www.youtube.com/iframe_api加载之后.

你可以在onYouTubeIframeAPIReady()里面创建更多玩家

var ytplayer1 = undef;
var ytplayer2 = undef;

function onYouTubeIframeAPIReady() {
    ytplayer1 = new YT.Player('player-youtube-1', {
        width: '640',
        height: '480',
        videoId: 'M7lc1UVf-VE'
    });

    ytplayer2 = new YT.Player('player-youtube-2', {
        width: '640',
        height: '480',
        videoId: 'smEqnnklfYs'
    });
}
Run Code Online (Sandbox Code Playgroud)

请注意,您需要声明ytplayer1和声明ytplayer2,onYouTubeIframeAPIReady()以便稍后使用它们:

ytplayer1.pauseVideo();
ytplayer2.playVideo();
Run Code Online (Sandbox Code Playgroud)