我遇到以下代码问题,但我不确定问题出在哪里。加载多个YouTube视频iFrame API
在我的母版页上,在javascript中,我定义了一个数组来定义我创建的YouTubePlayer
对象的列表。我也在这里加载YouTube API。
我也有(有时是几个)YouTube用户控件包含YouTube div的引导模式。它还包含一个JavaScript,用于将新的YouTubePlayer
对象推送到主页面js中的数组。最后,在用户控件上,我定义了用于在引导模式的“显示”和“隐藏”事件上自动启动和停止视频的方法。
最后,为了(希望)解决正在加载的文档和正在加载的YouTube API之间的竞争状态,我设置了两个bool变量(一个用于文档,一个用于API),并在调用init之前检查两个变量是否为true。函数,它遍历YouTubePlayer
对象数组并初始化它们,在窗口中设置YT.Player
对象。我认为问题的一部分是我无法静态设置window.player1
等,因为我不知道将会加载多少YouTube用户控件。
的问题是每当引导模式的事件发生火灾,YT.Player
对象我从窗口检索不包含playVideo()
和pauseVideo()
方法。
在我的母版页:
$(document).ready(function() {
window.docReady = true;
if (window.apiReady)
initVideos();
});
function onYouTubeIframeAPIReady() {
window.apiReady = true;
if (window.docReady)
initVideos();
initVideos();
}
function initVideos() {
if (typeof ytPlayerList === 'undefined')
return;
for (var i = 0; i < ytPlayerList.length; i++) {
var player = ytPlayerList[i];
var pl = new YT.Player(player.DivId, {
playerVars: {
'autoplay': '0',
'controls': '1',
'autohide': '2',
'modestbranding': '1',
'playsinline': '1',
'rel': '0',
'wmode': 'opaque'
},
videoId: player.VideoId,
events: {
'onStateChange': player.StateChangeHandler
}
});
window[player.Id] = pl;
}
}
而且用户控件:
window.ytPlayerList.push({
Id: "<%=ClientID%>player",
DivId: "<%=ClientID%>player",
VideoId: "<%=VideoId%>",
StateChangeHandler: hide<%=ClientID%>Player
});
function hide<%=ClientID %>Player(state) {
if (state.data == '0') {
hide<%=ClientID %>Video();
}
}
function show<%=ClientID %>Video() {
$('#<%=ClientID %>video-modal').modal('show');
}
function hide<%=ClientID %>Video() {
$('#<%=ClientID %>video-modal').modal('hide');
}
$(document).ready(function() {
$("#<%=ClientID%>Video").click(function() {
show<%=ClientID%>Video();
});
$("#<%=ClientID %>video-modal").on('shown', function() {
window["<%=ClientID%>player"].playVideo();
});
$("#<%=ClientID %>video-modal").on('hide', function() {
window["<%=ClientID%>player"].pauseVideo();
});
});
这可能是缺乏专业知识的js,但我绝对坚持。任何帮助将不胜感激。此外,作为参考,我得到的例外是
Uncaught TypeError: window.ctl100_phContent_ctl100player.playVideo is not a function
是否故意在'onYouTubeIframeAPIReady()'函数中激发'initVideos()'两次? –
可能重复[.playVideo() - 不是一个函数?](http://stackoverflow.com/questions/2217543/cant-control-youtube-embed-even-with-document-getelementbyidxyz-playvideo)或[playVideo doesn 't work](http://stackoverflow.com/questions/9287170/youtube-player-iframe-api-playvideo-doesnt-work-on-firefox-9-0-1) –
你可以粘贴一些生成的HTML请 ?这会让我更容易重现这个问题。 –