我希望使用YT.Player代码以便我可以检测事件。我有一个包含YouTube视频ID的CSV文件和一个将ID放入数组的函数,并且希望能够在用户单击图像时动态更改ID。基本上像这样:
html
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>
JavaScript
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "//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.
// NOTE: videoId is taken out and instead is placed as the generated IFRAME src from the postSelection function
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '100%',
width: '100%',
videoId: '<<CALL TO FUNCTION OR VARIABLE HERE>>',
events: {
//'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
如果您熟悉此代码,则会发生以下情况:#player
DIV 将被一个 IFRAME 替换。我可以使用此函数更改 IFRAME 源:function postSelection() {
$("#player").attr("src", _selected.attributes.url); //_selected.attributes.url comes from the CVS file
}
但是这个方法存在很多bug并且不支持跨浏览器。如果我使用标准的IFRAME而不是YT播放器API,一切都能正常工作。但是我想要检测结束,暂停等操作,所以必须使用API。我猜测这是一个状态问题,可能是在创建IFRAME的过程中某个地方出现了数据丢失。
谢谢。