onYouTubeIframeAPIReady called once but multiple videos needed on a page

前端 未结 3 1152
猫巷女王i
猫巷女王i 2020-12-11 21:04

I am using a server-side method to drop in YouTube videos with playlists and functioning buttons (think of a website widget that can be called anyway on a page, and potentia

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-11 21:15

    onYouTubeIframeAPIReady() is executed after YouTube API is ready to be used, that is after API's Javascript file http://www.youtube.com/iframe_api is loaded.

    You can create more players inside 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'
        });
    }
    

    Note that you need to declare ytplayer1 and ytplayer2 outside of onYouTubeIframeAPIReady() so you can use them later:

    ytplayer1.pauseVideo();
    ytplayer2.playVideo();
    

提交回复
热议问题