Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
问题描述
我已经与 youtube iframe api 斗争了很长一段时间了.不知何故,方法 onYouTubeIframeAPIReady
并不总是被触发.
I've been battling with the youtube iframe api for quite some time now. Somehow the method onYouTubeIframeAPIReady
is not always triggered.
从症状来看,似乎是加载问题.检查器中没有显示错误.
From the symptoms it seems a loading problem. No errors are shown in the inspector.
这是我的代码:
<div id="player"></div>
<script>
videoId = 'someVideoId';
var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
</script>
JS
(在页面末尾调用.我尝试将代码放在上面的脚本之后,结果是一样的.)
JS
(called at the end of the page. I tried to place the code right after the above script and the result was the same.)
var isReady = false
, player
, poster
, video;
$(function () {
$('.js-play').click(function (e) {
e.preventDefault();
interval = setInterval(videoLoaded, 100);
});
});
function onYouTubeIframeAPIReady() {
console.log(videoId)
player = new YT.Player('player', {
height: '445',
width: '810',
videoId: videoId,
events: {
'onReady': onPlayerReady//,
//'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
isReady = true;
console.log("youtube says play")
}
function videoLoaded (){
if (isReady) {
console.log("ready and play")
poster.hide();
video.show();
$('body').trigger('fluidvideos');
player.playVideo();
clearInterval(interval);
}
}
问题在于,有时 console.log
没有打印任何内容,也没有任何反应.
The problem is that sometimes nothing gets printed by the console.log
and nothing happens.
在手机上,这种情况一直都在发生.有什么想法吗?
On mobile phones this happens all the time. Any ideas?
推荐答案
这不是超时问题,你应该不需要手动触发这个函数.
It is not a timeout issue, and you should not need to fire this function manually.
确保您的 onYouTubeIframeAPIReady
函数在全局级别可用,而不是嵌套(隐藏)在另一个函数中.
Make sure your onYouTubeIframeAPIReady
function is available at the global level, not nested (hidden away) within another function.
这篇关于Youtube iframe api 未触发 onYouTubeIframeAPIReady的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Youtube iframe api 未触发 onYouTubeIframeAPIReady


基础教程推荐
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01