我正在尝试使用YouTube播放器API连接带有某些后端代码的嵌入式YouTube视频。我可以创建一个播放器对象,但是当状态就绪时,代码将无法识别我刚刚创建的对象。我在做什么错了?
<iframe id="playerLibrary" class="playerLocation" width="560" height="315" src="https://www.youtube.com/embed/YT2ZOD32lWw?rel=0&showinfo=0&enablejsapi=0" enablejsapi="1" frameborder="0" allowfullscreen></iframe>
jquery:
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player3;
function onYouTubeIframeAPIReady() {
player3 = new YT.Player('playerLibrary', {
events: {
'onReady': onPlayerReady(event),
//'onStateChange': onPlayerStateChange
},
playerVars: {
}
});
}
function onPlayerReady(event) {
console.log('ready'); //log: ready
console.log(player3); //log: undefined
//Hover play
$('.playerLocation').on('mouseover',function(){
player3.playVideo(); //log: Uncaught TypeError: player3.playVideo is not a function
});
//Blur Pause
$('.playerLocation').on('mouseout',function(){
player3.stopVideo(); //log: Uncaught TypeError: player3.stopVideo is not a function
});
}
答案 0 :(得分:0)
在您的events
参数中,它应该是'onReady': onPlayerReady
,而没有(event)
。
目前,onPlayerReady
函数在构建播放器时运行一次,但实际上尚未准备就绪。
在您的html中,&enablejsapi=0
也应更改为&enablejsapi=1