开发者

Is a Youtube buffer finish event possible

Is it possible to detect the finish of a youtube buffering through javascript? Here http://cod开发者_JAVA百科e.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".


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);
    }
}


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜