我创建了一个新页面,专门用来测试API。当复制粘贴他们的示例时
<div id="player"></div>
和
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
一切正常。
现在,当我删除<div id="player"></div>
并将其替换为
<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/GuVq-TZ7AJM?enablejsapi=1" frameborder="0"></iframe>
它不再起作用。它永远不会出现在onPlayerStateChange.或onPlayerReady中奇怪的是,它昨晚还能工作,今天就不行了。我附上了我的完整代码,以防万一它能帮上忙。
<!DOCTYPE html>
<html>
<body>
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/GuVq-TZ7AJM?enablejsapi=1" frameborder="0"></iframe>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://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', {
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
alert('test');
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>
发布于 2013-12-11 02:15:16
我在今天早些时候在我们的一个应用程序中也遇到了这个问题,我相信这是由于youtube中的一些内部更改/代码推送造成的。类似的事情也发生在6月份的c.f YouTube iframe player API - OnStateChange not firing
我解决了我的问题
<iframe id="ytplayer" ... src="http://www.youtube.com/embed/wQ78D6zoVKo"></iframe>
使用
<div id="ytplayer"></div>
从而让js脚本替换div。
线索就在你们的问题里,所以谢谢。
https://stackoverflow.com/questions/20501010
复制相似问题