2013-07-02 71 views
0

我嘗試使用5個以上的YouTube視頻播放器,但在創建第二個視頻播放器後,我遇到了一些問題。 當我創建第二個時,第一個消失,並且無法弄清楚什麼是錯的。擁有多個youtube API播放器?

會真的很感謝一些幫助 謝謝!

//slide 2 

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: '820', 
    width: '707', 
    videoId: 'NTq1WLKuOI4', 
    events: { 
    'onReady': onPlayerReady, 
    'onStateChange': onPlayerStateChange 
     } 
    }); 
    }  

    function onPlayerReady(event) { 
    setTimeout(function(){player.playVideo(); },8000); 
    } 

    // 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, 29000); 
    done = true; 
    } 
    } 

    function stopVideo() { 
    player.stopVideo(); 
    slide2(); 
    move(); 
    } 


var slide2 = function(){   
    setTimeout(function(){  
    slide3(); 
    move(); 
    },9000); 

} 


    //slide3 

    var player; 
    function onYouTubeIframeAPIReady() { 
    player2 = new YT.Player('player2', { 
    height: '820', 
    width: '707', 
    videoId: 'AQx1UjLv3Ps', 
    events: { 
    'onReady': onPlayerReady, 
    'onStateChange': onPlayerStateChange 
     } 
    }); 
    }  

    function onPlayerReady(event) { 
    setTimeout(function(){player2.playVideo(); },8000); 
    } 

    // 5. The API calls this function when the player's state changes. 
    var done = false; 
    function onPlayerStateChange(event) { 
    if (event.data == YT.PlayerState.PLAYING && !done) { 
    setTimeout(stopVideo2, 29000); 
    done = true; 
    } 
    } 

    function stopVideo2() { 
    player2.stopVideo(); 

    } 
+0

我無法弄清楚這一點。任何人? –

+0

想出來 - [解決方案](http://stackoverflow.com/questions/17012886/loading-multiple-video-players-with-youtube-api) –

+0

可能重複的[第二個視頻保持循環,被調用後第一個結束 - youtube API?](http://stackoverflow.com/questions/17441532/second-video-keeps-looping-after-is-being-called-on-the-firsts-end-youtube-ap) –

回答

1

您不能多次調用onYouTubeIframeAPIReady()。您需要在一個onYouTubeIframeAPIReady呼叫中初始化所有玩家。嘗試將onYouTubeIframeAPIReady的內容封裝到新的命名函數中,然後在onYouTubeIframeAPIReady中調用它們。

相關問題