我已经查看了很多问题和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准备好或其他任何东西。有任何想法吗?感谢
答案 0 :(得分:31)
回调函数必须位于全局范围内。只需将onYouTubeIframeAPIReady
和其他人移到callYTapi
之外。
答案 1 :(得分:5)
对我来说幸运的是,经过大量的控制台试验后,我得到了具体的东西。直到一天前我才遇到同样的问题,并且在我处于单身范围内时想出了怎么做。
以下是我的代码:
var XT = XT || {};
window.onYouTubeIframeAPIReady = function(){
setTimeout(XT.yt.onYouTubeIframeAPIReady,500);
}
XT.yt = {
/* load the YouTube API first */
loadApi: function () {
var j = document.createElement("script"),
f = document.getElementsByTagName("script")[0];
j.src = "//www.youtube.com/iframe_api";
j.async = true;
f.parentNode.insertBefore(j, f);
console.log('API Loaded');
},
/*default youtube api listener*/
onYouTubeIframeAPIReady: function () {
console.log('API Ready?');
window.YT = window.YT || {};
if (typeof window.YT.Player === 'function') {
player = new window.YT.Player('player', {
width: '640',
height: '390',
videoId: 'nE6mDCdYuwY',
events: {
onStateChange: XT.yt.onPlayerStateChange,
onError: XT.yt.onPlayerError,
onReady: setTimeout(XT.yt.onPlayerReady, 4000)
}
});
}
},
onPlayerReady: function() {
player.playVideo(); /* start the video */
player.setVolume(1); /* set volume to 1 (accepts 0-100) */
},
onPlayerStateChange: function (e) {
console.log(e.data, YT.PlayerState.PLAYING, e.data === YT.PlayerState.PLAYING);
var video_data = e.target.getVideoData();
//do something on video ends
if(e.data === YT.PlayerState.ENDED)
this.onPlayerStop();
},
onPlayerStop: function(){
//console.log('video ended');
},
onPlayerError: function (e) {
console.log( "youtube: " + e.target.src + " - " + e.data);
},
init: function () {
this.loadApi();
}
}
XT.yt.init();
答案 2 :(得分:3)
为了清楚起见,上面接受的答案似乎对我有用:
加载API(来自yt文档)
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
在窗口中指定YouTube的内置就绪功能“onYouTubeIframeAPIReady”
window.onYouTubeIframeAPIReady = this.onYTready;
所以this.onYTready
是我的函数名,位于其他地方。
我可以确认我的onYTready
函数可以控制Chrome中的日志/调试程序断点。
(编辑)您可能希望将其绑定到您的函数,例如:
window.onYouTubeIframeAPIReady = this.onYTready.bind(this);
或'this'将用于窗口,而不是您可能正在使用的特定视图..
答案 3 :(得分:0)
function playIframeVideo(iframe) {
iframe.videoPlay = true; //init state
iframe.contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*');
}
function pauseIframeVideo(iframe) {
iframe.videoPlay = false; //init state
iframe.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
}
$('.vc_video-bg-container').on('click', function () {
var iframe = $('iframe').get(0); //iframe tag
if (iframe.videoPlay != true) {
playIframeVideo(iframe);
} else {
pauseIframeVideo(iframe);
}
});