预加载 YouTube 嵌入内容

发布于 2024-12-15 01:08:48 字数 133 浏览 2 评论 0原文

我想要一个嵌入式 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

看海 2024-12-22 01:08:48

我有同样的问题并遇到了这个问题。经过一番研究,我想我找到了一个更清晰但相似的答案。

当 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 to onStateChange 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.

执妄 2024-12-22 01:08:48

如果我们查看此内容: https://developer.mozilla.org/en -US/docs/Web/HTML/Preloading_content

预加载 Iframe 可能会有所帮助:

    <link rel="preload" href="https://www.youtube.com/embed/dQw4w9WgXcQ" as="document">

If we view this: https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content

Preloading the Iframe may help:

    <link rel="preload" href="https://www.youtube.com/embed/dQw4w9WgXcQ" as="document">
傲世九天 2024-12-22 01:08:48

调用

player.cueVideoById(videoId:String);

而不是

player.loadVideoById(videoId:String);

Call

player.cueVideoById(videoId:String);

Instead of

player.loadVideoById(videoId:String);
冰葑 2024-12-22 01:08:48

我正在寻找此问题的解决方案并浏览了这篇文章:

嵌入 YouTube 视频在不增加加载时间的情况下做出响应

摘要指出:此方法会将网页大小减少 300-400 KB,同时使您的网站适合移动设备。

将其粘贴到页面上

<div class="youtube-container">
<div class="youtube-player" data-id="VIDEOID"></div>
</div>

javascript

<script>
(function() {
    var v = document.getElementsByClassName("youtube-player");
    for (var n = 0; n < v.length; n++) {
        var p = document.createElement("div");
        p.innerHTML = labnolThumb(v[n].dataset.id);
        p.onclick = labnolIframe;
        v[n].appendChild(p);
    }
})();

function labnolThumb(id) {
    return '<img class="youtube-thumb" src="//i.ytimg.com/vi/' + id + '/hqdefault.jpg"><div class="play-button"></div>';
}

function labnolIframe() {
    var iframe = document.createElement("iframe");
    iframe.setAttribute("src", "//www.youtube.com/embed/" +
        this.parentNode.dataset.id + "?  autoplay=1&autohide=2&border=0&wmode=opaque&enablejsapi=1&controls=0&showinfo=0");
    iframe.setAttribute("frameborder", "0");
    iframe.setAttribute("id", "youtube-iframe");
    this.parentNode.replaceChild(iframe, this);
}
</script>

CSS

<style>
.youtube-container {
    display: block;
    margin: 20px auto;
    width: 100%;
    max-width: 600px;
}
.youtube-player {
    display: block;
    width: 100%;
    /* assuming that the video has a 16:9 ratio */

    padding-bottom: 56.25%;
    overflow: hidden;
    position: relative;
    width: 100%;
    height: 100%;
    cursor: hand;
    cursor: pointer;
    display: block;
}
img.youtube-thumb {
    bottom: 0;
    display: block;
    left: 0;
    margin: auto;
    max-width: 100%;
    width: 100%;
    position: absolute;
    right: 0;
    top: 0;
    height: auto
}
div.play-button {
    height: 72px;
    width: 72px;
    left: 50%;
    top: 50%;
    margin-left: -36px;
    margin-top: -36px;
    position: absolute;
    background: url("http://i.imgur.com/TxzC70f.png") no-repeat;
}
#youtube-iframe {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
}
</style>

请参阅原始文章评论以获取其他修改建议和改进。

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:

<div class="youtube-container">
<div class="youtube-player" data-id="VIDEOID"></div>
</div>

The javascript:

<script>
(function() {
    var v = document.getElementsByClassName("youtube-player");
    for (var n = 0; n < v.length; n++) {
        var p = document.createElement("div");
        p.innerHTML = labnolThumb(v[n].dataset.id);
        p.onclick = labnolIframe;
        v[n].appendChild(p);
    }
})();

function labnolThumb(id) {
    return '<img class="youtube-thumb" src="//i.ytimg.com/vi/' + id + '/hqdefault.jpg"><div class="play-button"></div>';
}

function labnolIframe() {
    var iframe = document.createElement("iframe");
    iframe.setAttribute("src", "//www.youtube.com/embed/" +
        this.parentNode.dataset.id + "?  autoplay=1&autohide=2&border=0&wmode=opaque&enablejsapi=1&controls=0&showinfo=0");
    iframe.setAttribute("frameborder", "0");
    iframe.setAttribute("id", "youtube-iframe");
    this.parentNode.replaceChild(iframe, this);
}
</script>

The CSS:

<style>
.youtube-container {
    display: block;
    margin: 20px auto;
    width: 100%;
    max-width: 600px;
}
.youtube-player {
    display: block;
    width: 100%;
    /* assuming that the video has a 16:9 ratio */

    padding-bottom: 56.25%;
    overflow: hidden;
    position: relative;
    width: 100%;
    height: 100%;
    cursor: hand;
    cursor: pointer;
    display: block;
}
img.youtube-thumb {
    bottom: 0;
    display: block;
    left: 0;
    margin: auto;
    max-width: 100%;
    width: 100%;
    position: absolute;
    right: 0;
    top: 0;
    height: auto
}
div.play-button {
    height: 72px;
    width: 72px;
    left: 50%;
    top: 50%;
    margin-left: -36px;
    margin-top: -36px;
    position: absolute;
    background: url("http://i.imgur.com/TxzC70f.png") no-repeat;
}
#youtube-iframe {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
}
</style>

Refer to the original article comments for additional modification suggestions and improvements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文