YouTube播放器无法识别嵌入式YouTube视频 [英] YouTube player doesn't recognize embedded YouTube video
问题描述
我正在尝试使用YouTube播放器API连接带有某些后端代码的嵌入式YouTube视频.我可以创建一个播放器对象,但是当状态就绪时,代码将无法识别我刚刚创建的对象.我在做什么错了?
I'm trying to hook up an embedded YouTube video with some backend code using the YouTube player api. I'm able to create a player object, but then when the state is ready, the code doesn't recognize the object I just created. What am I doing wrong?
<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
});
}
推荐答案
在您的events
参数中,它应该是'onReady': onPlayerReady
,而没有(event)
.
In your events
param, it should be 'onReady': onPlayerReady
, without the (event)
.
当前,onPlayerReady
函数在构建播放器时运行一次,但实际上尚未准备就绪.
Currently you onPlayerReady
function is run once when the player is being constructed, but it is not actually ready.
也在您的html中,&enablejsapi=0
应该更改为&enablejsapi=1
Also in your html, &enablejsapi=0
should be changed to &enablejsapi=1
这篇关于YouTube播放器无法识别嵌入式YouTube视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!