现在,我尝试使用YouTube API进行简单操作。除了我在API中没有得到很多东西之外,我只想了解一件简单的事情:为什么我的视频没有显示在我的jsfiddle中,尽管如此,我确认,我复制了来自另一个jsfiddle的整个代码,并没有改变一件事。
这是jsfiddle:http://jsfiddle.net/jpreynat/e11oy0eu/
我的最终目标是使用简单的JQuery在单击播放或暂停时向主体添加和删除类。这是我的最终代码:https://jsfiddle.net/us64a5os
// 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.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
function onPlayerStateChange(event) {
switch(event.data) {
case 1:
$('body').addClass('play');
break;
case 2:
$('body').removeClass('play');
}
}
查看控制台中的不同错误,我尝试将https设置为http或仅设置为//,我删除了Adblock,我尝试了桌面上的本地文件等等。
谢谢=)
答案 0 :(得分:0)
您应该在窗口对象中使用 onYouTubePlayerAPIReady 功能
window.onYouTubePlayerAPIReady = function() {
console.log('ready now...');
}
这应该有效!