iframe / Javascript 检查 Youtube 视频是否结束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9900747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:10:12  来源:igfitidea点击:

Iframe / Javascript Check if Youtube Video Ends

javascriptyoutubeevent-listener

提问by Howdy_McGee

Right now I have youtube videos loading in via Iframe. Is it even possible to check if the youtube video ended when it is in that sort of state? Can I use OnStateChange() through the Iframe?

现在我通过 iframe 加载了 youtube 视频。甚至可以检查 YouTube 视频在这种状态下是否结束?我可以通过 Iframe 使用 OnStateChange() 吗?

采纳答案by Kai

回答by irishdan

you can detect the end of the video like so:

您可以像这样检测视频的结尾:

function onPlayerStateChange(event) {
  if (event.data == YT.PlayerState.ENDED){
    // your code to run after video has ended
  }
}

回答by alex

If you have an iframeon your page, and its srcattribute contains a URL which does not have the same domain, protocol or port as the container page, you can not access its documentbecause of Same Origin Policy.

如果您有iframe您的网页上,它的src属性包含不具有相同的域,协议或端口作为容器页面的URL,你不能访问它document的,因为同源策略

You may want to look into YouTube supporting postMessage().

您可能需要查看 YouTube 支持postMessage().

If you do own the iframe's contents, then get its iframe.contentDocumentwhere you can access the iframe's document.

如果您确实拥有iframe's 的内容,那么请iframe.contentDocument在您可以访问iframe's 的地方获取它document