幾個月前我發佈了一個關於this的問題,我的問題解決了。幾天前,我得到了報告,這個問題再次發生,我不確定爲什麼。YT.Player有空值
我想創建一個YT.Player對象,但失敗。當我console.log()YT.Player對象,我沒有看到相關的預期功能,如cuePlaylist()
或getDuration()
。通過我的調試,我成功地提取了Youtube Video ID(我有console.log),並在創建YT.Player對象時將其作爲參數傳遞。我不明白爲什麼YT.Player會在我傳遞一個有效的YouTube ID時報告一個空視頻。此外,我已確保我的iframe
附有YouTube視頻的ID。
onYouTubeIframeAPIReady = function() {
createPpdYoutubeObjects();
};
function createPpdYoutubeObjects() {
var delay = 5000; // need to wait for Youtube videos to load
setTimeout(function(){
// Sets up player tracker, and init the carousel
var players={};
$('iframe.ytplayer').each(function() {
players[ grabYoutubeIdFromUrl($(this).attr('src')) ] = new YT.Player(grabYoutubeIdFromUrl($(this).attr('src')), {
events: {
'onReady': onReady,
'onStateChange': onStateChange
}
});
console.log('id: ' + grabYoutubeIdFromUrl($(this).attr('src')));
console.log(players[ grabYoutubeIdFromUrl($(this).attr('src')) ]);
});
}, delay);
};
function grabYoutubeIdFromUrl(path) {
if (
typeof path === "string"
&& path.length > 0
&& path.indexOf('embed/') > -1
&& path.indexOf('?wmode', path.indexOf('embed/')) > -1
)
{
var start = path.indexOf('embed/') + 6;
var end = path.indexOf('?wmode', start);
return path.substring(start, end);
}
return "";
};
<iframe id="eEIWYgA2lbQ" class="ytplayer" type="text/html" width="930" height="524"
src="https://www.youtube.com/embed/9VZUcLgtDM4?wmode=opaque&rel=0&enablejsapi=1&iv_load_policy=3"
frameborder="0" allowfullscreen=""></iframe>
<iframe id="L7oamJtBpdU" class="ytplayer" type="text/html" width="930" height="524"
src="https://www.youtube.com/embed/lF1j8mdmVEI?wmode=opaque&rel=0&enablejsapi=1&iv_load_policy=3"
frameborder="0" allowfullscreen=""></iframe>