gpt4 book ai didi

javascript - Youtube 嵌入式 iframe onReady 和 onStateChange 事件未在 Internet Explorer 中触发

转载 作者:数据小太阳 更新时间:2023-10-29 04:21:33 26 4
gpt4 key购买 nike

我复制了Youtube内嵌播放器的演示代码Youtube embedded player demo code几乎一字不差 - 更改是插入 console.log 以便我可以看到发生了什么。

    <!DOCTYPE html>
<html>
<body>
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>

<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', {
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) {
console.log('onPlayerReady');
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) {
console.log('onPlayerStateChange');
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>

这在 Safari 和 Firefox 中按预期工作,即一旦 API 加载选择的视频播放 6 秒并且 console.log 包含事件正在触发的证据。但是在 Internet Explorer 中,onReady 和 onStateChange 事件不会触发。如果我手动单击播放器中的播放控件,视频将播放,但我没有收到预期的 onStateChange 事件。

我检查了我机器上的 Flash 版本,它是:11.2.202.228,当前可用的是 12.0.0.70。如果我在 IE 中禁用 Flash(管理附加组件),播放器将按预期工作:播放 6 秒并触发事件。

我没有更新 Flash,因为虽然这可能会解决我机器上的问题,但我的观众将仅限于那些可以做同样事情的人,如果我更新了,我将无法确定问题是否已被其他人解决意味着。

我也曾尝试自己手动创建 iframe,如上述 Youtube 文档中所述,这在 Firefox 和 Safari 中按预期工作但在 IE 中没有。

有一个Youtube demo player 这在 Firefox 中完美运行,通过 API 工作的播放、暂停和停止控件以及事件在“统计”部分的屏幕上写入事件和函数调用时清楚地触发。相比之下,当我在 IE 中查看此内容时,通过 API 进行的播放、暂停和停止控件什么都不做。统计部分没有输出。播放和暂停的唯一方法是使用播放器上的按钮。如果我禁用 Flash,那么播放器和 API 将完美运行。

如果我尝试嵌入非 API 的 iframe,那么播放器可以正常工作,这让我相信这是使用 API 引起的问题。

我知道有些人会说所有用户都应该拥有最新的 Flash,说起来容易做起来难。许多学校、公司和图书馆都锁定了 IE 中的选项部分,以便用户能够为我的网站关闭 Flash。

我想知道在使用 API 时有什么方法可以关闭 Flash 吗?

我在 Stackoverflow 和其他地方进行了搜索,虽然我发现了类似的问题,但我没有找到任何修复方法。 I did find a somewhat similar promising lead但唉,我没有快乐。 Also hereherehereherehere .

我必须指出,在本地机器和网站上使用时,即使我设置了 origin 参数,问题仍然存在。

我将不胜感激建议和帮助。提前致谢。

最佳答案

I wonder is there some way of turning off Flash when using the API?

您可以使用以下选项强制 YouTube 播放器使用其 html5 播放器:

playerVars: {html5: 1}

将您的代码更改为:

// 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',
playerVars: {
html5: 1
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}

关于javascript - Youtube 嵌入式 iframe onReady 和 onStateChange 事件未在 Internet Explorer 中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22227737/

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