预加载 YouTube 嵌入内容
我想要一个嵌入式 chromeless youtube 视频预加载其视频,而不是在页面加载时播放。现在我正在使用一个尴尬的“播放然后快速暂停”脚本,这会导致一些小问题(半秒音频泄漏并且失败很多)。对于这个看似简单的功能,是否有更好/更优雅的预加载方式?
I want to have an embedded chromeless youtube video preload its video WITHOUT playing when the page loads. Right now I'm using an awkward "play then quickly pause" script which causes small problems (half-second audio leaks and fails quite a bit). For this seemingly simple functionality, is there a better/more elegant way to preload?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我有同样的问题并遇到了这个问题。经过一番研究,我想我找到了一个更清晰但相似的答案。
当 JavaScript API 调用
OnYouTubePlayerReady
时,您按下播放并向onStateChange
添加一个事件侦听器,每次播放器从缓冲变为播放时都会调用该事件侦听器。例如,在函数内,您监听状态 3(正在缓冲),一旦调用它,您就暂停视频。
您可以在 这个 jsFiddle 中看到该技术的实际应用。
旁注:我在示例中没有使用 JavaScript 框架,但您可以轻松地在此处放置一个框架。
另外,我无法使用 jsFiddle 从 HTML 正文中提取脚本标记,但外部
script.js
文件在我自己的服务器上运行得很好。I had the same question and came across this question. After some research, I think I found a cleaner, albeit similar, answer.
When the JavaScript API calls
OnYouTubePlayerReady
, you press play and add an event listener toonStateChange
that will be called every time the player changes from buffering to play.For example, inside the function you listen for state 3, which is buffering, and as soon as it's called, you pause the video.
You can see this technique in action in this jsFiddle.
Side note: I refrained from using a JavaScript framework in my example, but you could easily put one into place here.
Also, I was unable to abstract the script tag out of the body of the HTML using jsFiddle, but an external
script.js
file works just fine on my own server.如果我们查看此内容: https://developer.mozilla.org/en -US/docs/Web/HTML/Preloading_content
预加载 Iframe 可能会有所帮助:
If we view this: https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content
Preloading the Iframe may help:
调用
而不是
Call
Instead of
我正在寻找此问题的解决方案并浏览了这篇文章:
嵌入 YouTube 视频在不增加加载时间的情况下做出响应
摘要指出:此方法会将网页大小减少 300-400 KB,同时使您的网站适合移动设备。
将其粘贴到页面上:
javascript:
CSS:
请参阅原始文章评论以获取其他修改建议和改进。
I was looking for a solution to this problem and ran across this article:
Embed YouTube Videos Responsively without Increasing Load Time
The summary states: This method will reduce the size of your webpages by 300-400 KB while making your site mobile friendly.
Paste this on the page:
The javascript:
The CSS:
Refer to the original article comments for additional modification suggestions and improvements.