我已经查看了很多问题和 youtube api 的东西,但我一生都无法弄清楚为什么 onYouTubeIframeAPIReady 不起作用。
这是我的 iframe:
<iframe id="youtube_vid" width="763" height="430" src="https://www.youtube.com/embed/dlJshzOv2cw?theme=light&showinfo=0&rel=0&enablejsapi=1" frameborder="0" allowfullscreen=""></iframe>
还有我的脚本:
function callYTapi() {
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
console.log('script loaded');
function onYouTubeIframeAPIReady() {
console.log('IframeAPI = Ready');
var player = new YT.Player('youtube_vid', {
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
event.target.playVideo();
}
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PAUSED) {
console.log("Paused");
}
if (event.data == YT.PlayerState.PLAYING) {
console.log("Playing");
}
if (event.data == YT.PlayerState.ENDED) {
end();
}
}
}
我得到了加载脚本的console.log,但没有准备好Iframe或其他任何东西。有任何想法吗?谢谢