javascript 如何控制基于 <iframe> 的 YouTube 播放器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6228923/
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
How to control <iframe> based YouTube player?
提问by tzador
YouTube allows to use <iframe>
to embed videos on sites in addition to flash based way of embedding. That has advantages especially for mobile devices.
http://apiblog.youtube.com/2010/07/new-way-to-embed-youtube-videos.html
<iframe>
除了基于 Flash 的嵌入方式外,YouTube 还允许使用在网站上嵌入视频。这尤其对于移动设备具有优势。
http://apiblog.youtube.com/2010/07/new-way-to-embed-youtube-videos.html
Unfortunatly i could not find any API documentation similar to flash based way like http://code.google.com/apis/youtube/js_api_reference.html
不幸的是,我找不到任何类似于基于 Flash 的方式的 API 文档,例如http://code.google.com/apis/youtube/js_api_reference.html
Is it possible to control programmatically the <iframe>
based player using pure JavaScript, for example playing or pausing the video?
是否可以<iframe>
使用纯 JavaScript以编程方式控制基于播放器,例如播放或暂停视频?
采纳答案by Niklas
https://developers.google.com/youtube/iframe_api_reference#Operations
https://developers.google.com/youtube/iframe_api_reference#Operations
We support a similar set of functions for the IFrame API as are currently supported for the JavaScript API.
Please refer to that document for a list of functions. Please note that the functions that deal with video bytes behave differently when HTML5 playback is used via the IFrame API. getVideoBytesTotal is hardcoded to return 1000. getVideoBytesLoaded will return a value between 0 and 1000. To calculate the fraction of the video that has been loaded you can divide the getVideoBytesLoaded value by the getVideoBytesTotal value, and that calculation will work regardless of whether HTML5 or ActionScript 3 playback is used.
我们为 IFrame API 支持一组与 JavaScript API 当前支持的类似功能。
请参阅该文档以获取功能列表。请注意,当通过 IFrame API 使用 HTML5 播放时,处理视频字节的函数的行为会有所不同。getVideoBytesTotal 被硬编码为返回 1000。getVideoBytesLoaded 将返回一个介于 0 和 1000 之间的值。要计算已加载视频的分数,您可以将 getVideoBytesLoaded 值除以 getVideoBytesTotal 值,无论是 HTML5 还是 ActionScript,该计算都将起作用3 回放被使用。
Do note it is a experimental service which should not be used for production level applications.
请注意,这是一项实验性服务,不应用于生产级应用程序。
Notice
注意
Important: This is an experimental feature, which means that it might change unexpectedly.
重要提示:这是一项实验性功能,这意味着它可能会意外更改。