gpt4 book ai didi

php - YouTube iframe API 触发所有事件两次

转载 作者:可可西里 更新时间:2023-11-01 01:05:05 24 4
gpt4 key购买 nike

虽然 YouTube API 使用 embedswf 工作正常, 我的脚本在使用 iframes 时无法正常工作嵌入播放器。更具体地说,API 的 iframe 版本似乎会触发所有事件两次,从而导致各种问题,例如本例中的问题 (hosted):

<html>

<head>
<script type="text/javascript" src="http://www.youtube.com/iframe_api"></script>
<script src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
</head>

<body>

This script initializes a YouTube API player in an iframe which cycles through the array an video IDs. Every time a video ends, the next video ID in the array is retrieved and its video is played. <b>Unfortunately, the player reports the ending of a video twice, which results in every other video being skipped</b>.

<pre>

<?php
$videos = array("TLU2DZqhsSs","8CJn4T5ulL8","gdzf5oQJWxo","VTxKMiPXOGU","BeuwxSHHFh8");
print_r($videos);
?>

</pre>

<div id="ytapiplayer">This text will be replaced by a player.</div>

<script type="text/javascript">
playlistids = <?php echo json_encode($videos); ?>;

var playing = playlistids[0];

var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

var ytplayer;

function onYouTubeIframeAPIReady() {
ytplayer = new YT.Player('ytapiplayer', {
width: '922',
height: '522',
videoId: playing,
events: {
'onStateChange': onytplayerStateChange,
'onReady': onPlayerReady
}
});
}

function onPlayerReady(event) {
event.target.setPlaybackQuality('hd720')
event.target.setVolume(100)
alert('Start playing entry #0 (preloaded).')
event.target.playVideo()
}

function play(ytid) {
if (ytplayer) {
playing = ytid
alert('On to entry #'+ nextentrykey +', playing set to: ' + playing)
ytplayer.loadVideoById(ytid, 0, "hd720");
}
}

function onytplayerStateChange(event) {
//alert('NEW STATE: ' + event.data)
if ( event.data == 0 ) {
alert('Since the new player state is '+ event.data +', the video has ended. Getting next key after playing ' + playing + '.');
nextentrykey = parseInt(playlistids.getKey(playing))+1
if (nextentrykey >= playlistids.length) {
nextentrykey = 0
}
play(playlistids[nextentrykey]);
}
}

Object.prototype.getKey = function(value){
for(var key in this){
if(this[key] == value){
return key;
}
}
return -1;
};
</script>

</body>

</html>

我是否应该忽略从 API 收到的所有其他调用并实现解决方法?还是可以用更明智的方式解决这个问题?

感谢任何帮助!

最佳答案

我认为问题是你加载了两次 iframe_api 库

并在 YouTubeIframeAPIReady 上运行两次

尝试删除这些行:

var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

关于php - YouTube iframe API 触发所有事件两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12989015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com