2017-05-07 41 views
1

我顯示了YouTube視頻的鏈接列表。在YT.player中播放視頻

document.getElementById('videos').innerHTML += "<div>" 
        + "<a href='#' onclick='showVideo(\"" + videoid + "\"); return false'>" + videotitle + "</a>" + videodescription + "" 
        + "</div>"; 

點擊任何鏈接,並在其div中播放視頻。

function showVideo(arg) { 
console.log(arg); 

    var player; 

       player = new YT.Player('response', { 
        videoId: arg, 
        // height: '700', 
        // width: '1000', 
        playerVars: { 
         controls: 1, // 1 is default value 
         autoplay: 1, 
         disablekb: 0, // 0 is the default value 
         enablejsapi: 1, 
         iv_load_policy: 3, 
         modestbranding: 1, 
         showinfo: 0, 
         rel: 0, 
         fs: 0 //Setting this parameter to 0 prevents the fullscreen button from displaying in the player. 
         //The default value is 1, which causes the fullscreen button to display. 
        } 
       }) 
     //   player.destroy() 

} 

我點擊從顯示的列表選擇不同的鏈接,我期待現有的視頻與新的視頻所取代,但這種情況不會發生,仍然顯示第一視頻。 是不是變量播放器再次實例化? 我需要做些什麼才能做到這一點?

回答

0

嘗試使用loadVideoById

This function loads and plays the specified video.

語法:

player.loadVideoById(videoId:String, 
        startSeconds:Number, 
        suggestedQuality:String):Void 

這樣實現的:

$(".button").on("click", function() { 
     player.loadVideoById("Vw4KVoEVcr0", 0, "default"); 
    }); 

變量player是你new YT.Player(),這段代碼將LO將新的videoID = Vw4KVoEVcr0添加到YT.player

參考:

希望這有助於。

+0

是的,它幫助我嘗試別的。我用_onYouTubeIframeAPIReady()_與你的代碼相同,但在我的函數_showVideo(arg)_我把_player.loadVideoById(arg,0,「default」); _和我得到的錯誤是_Uncaught TypeError:player.loadVideoById不是功能_ – zen