是否可能发生 YouTube 缓冲区完成事件

发布于 2024-11-25 12:38:26 字数 220 浏览 0 评论 0原文

是否可以通过javascript检测youtube缓冲的完成情况?这里 http://code.google.com/intl/de-DE/apis/youtube /js_api_reference.html 有很多方法,但没有人有一个事件表明“缓冲完成”。

Is it possible to detect the finish of a youtube buffering through javascript? Here http://code.google.com/intl/de-DE/apis/youtube/js_api_reference.html are a lot of methods but no one has an event that says "finished with buffering".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

动听の歌 2024-12-02 12:38:26
var ytplayer;

function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById("myytplayer");
    checkBuffer();
}

function checkBuffer(){
    if(ytplayer.getVideoBytesLoaded() == ytplayer.getVideoBytesTotal()){
        alert('Buffer Complete!');
    }else{
        var t = setTimeout(function(){
            Editor.split();
        },1000);
    }
}
var ytplayer;

function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById("myytplayer");
    checkBuffer();
}

function checkBuffer(){
    if(ytplayer.getVideoBytesLoaded() == ytplayer.getVideoBytesTotal()){
        alert('Buffer Complete!');
    }else{
        var t = setTimeout(function(){
            Editor.split();
        },1000);
    }
}
紫竹語嫣☆ 2024-12-02 12:38:26

我知道这个问题已经很老了,但答案可能对某些人仍然有帮助:

YouTube 视频可以处于 6 种状态之一:

  • -1 – 未开始
  • 0 – 结束
  • 1 – 播放
  • 2 – 暂停
  • 3 – 缓冲
  • 5 – 视频提示

当此状态时更改(即当视频停止缓冲并进入“暂停”或“播放”状态时),将触发“onStateChange”事件。因此,请跟踪先前的状态和新的状态。当前一个状态是“正在缓冲”并且新状态是“结束”、“播放”或“暂停”时,这意味着视频已完成缓冲。

这是一个例子:

<html>
<body>
  <div id="player"></div>
  <script>
    var player;
    var lastState;

    var tag = document.createElement('script');

    tag.src = "https://www.youtube.com/iframe_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

    var player;
    function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
            height: '390',
            width: '640',
            videoId: 'M7lc1UVf-VE',
            events: {
                'onStateChange': onPlayerStateChange
            }
        });
    }


    function onPlayerStateChange(event) {
        if (lastState == YT.PlayerState.BUFFERING &&
            (event.data == YT.PlayerState.PLAYING || event.data == YT.PlayerState.PAUSED)) {
            alert('Buffering has completed!');
        }
        lastState = event.data;
    }
  </script>
</body>
</html>

I know this question is old but the answer may still be helpful to some:

The YouTube video can be in one of 6 states:

  • -1 – unstarted
  • 0 – ended
  • 1 – playing
  • 2 – paused
  • 3 – buffering
  • 5 – video cued

When this state changes (i.e. when the video stops buffering and enters a 'paused' or a 'played' state), the "onStateChange" event is triggered. So, keep track of the previous state and the new state. When the previous state is 'buffering' and the new state is 'ended', 'playing', or 'paused', then this means that the video finished buffering.

Here is an example:

<html>
<body>
  <div id="player"></div>
  <script>
    var player;
    var lastState;

    var tag = document.createElement('script');

    tag.src = "https://www.youtube.com/iframe_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

    var player;
    function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
            height: '390',
            width: '640',
            videoId: 'M7lc1UVf-VE',
            events: {
                'onStateChange': onPlayerStateChange
            }
        });
    }


    function onPlayerStateChange(event) {
        if (lastState == YT.PlayerState.BUFFERING &&
            (event.data == YT.PlayerState.PLAYING || event.data == YT.PlayerState.PAUSED)) {
            alert('Buffering has completed!');
        }
        lastState = event.data;
    }
  </script>
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文