YouTube (injected) video ends callback

11,804

A working example of the below code is also on jsfiddle.net.

Some notes:

  • Uses the iframe_api, not the javascript_api
  • The YT.Player constructor is minimal because you are building the iframe yourself.
  • The "playerVars" are included as iframe URL parameters.
  • The parameter "enablejsapi=1" is set.

Example Markup

<script src="http://www.youtube.com/iframe_api"></script>

<a style="display: block;" class="video-thumb" id="HuGbuEv_3AU" href="#">
    Megadeth: Back In The Day
</a>  
<a style="display: block;" class="video-thumb" id="jac80JB04NQ" href="#">
    Judas Priest: Metal Gods
</a> 
<a style="display: block;" class="video-thumb" id="_r0n9Dv6XnY" href="#">
    Baltimora: Tarzan Boy
</a>   

<div id="container"></div>
<div id="log">--Logging enabled--</div>

The JavaScript

function log(msg) {
    jQuery('#log').prepend(msg + '<br/>');
}

function onPlayerStateChange(event) {
    switch(event.data) {
        case YT.PlayerState.ENDED:
            log('Video has ended.');
            break;
        case YT.PlayerState.PLAYING:
            log('Video is playing.');
            break;
        case YT.PlayerState.PAUSED:
            log('Video is paused.');
            break;
        case YT.PlayerState.BUFFERING:
            log('Video is buffering.');
            break;
        case YT.PlayerState.CUED:
            log('Video is cued.');
            break;
        default:
            log('Unrecognized state.');
            break;
    }
}

jQuery(document).ready(function($) {
    $('.video-thumb').click(function() {

        var vidId = $(this).attr('id');
        $('#container').html('<iframe id="player_'+vidId+
            '" width="420" height="315" src="http://www.youtube.com/embed/'+
            vidId+'?enablejsapi=1&autoplay=1&autohide=1&showinfo=0" '+
            'frameborder="0" allowfullscreen></iframe>');

        new YT.Player('player_'+vidId, {
            events: {
                'onStateChange': onPlayerStateChange
            }
        });
    });
});
Share:
11,804
Jeaf Gilbert
Author by

Jeaf Gilbert

Full-stack Developer (PHP, HTML, CSS, Javascript, MySQL)

Updated on June 03, 2022

Comments

  • Jeaf Gilbert
    Jeaf Gilbert almost 2 years

    I inject YouTube iframe to a div in document ready and bind it to click:

    jQuery(document).ready(function($) {
        jQuery('.video-thumb').click(function(){
            ...
            $('#player').html('<iframe width="761" height="421" src="http://www.youtube.com/embed/' + $(this).attr('videoid') + '?rel=0&wmode=transparent" frameborder="0" allowfullscreen></iframe>');
            ...
        }
     }
    

    And I want to do a callback when video ends. I have read about onYouTubePlayerAPIReady, but it has to be put outside document ready.

    I have tried to load video player by this construction outside document ready:

    var player;
    function onYouTubePlayerAPIReady() {
        player = new YT.Player('player', {
          height: '421',
          width: '761',
          videoId: '',
          playerVars: { autoplay: 1, autohide: 1, showinfo: 0 },
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
    }
    

    but I got some issues:

    • showinfo:0 didn't work, still got other video thumbnails in the end
    • I don't know how to change video id (and reinit the video?)
    • more script errors than first method (injecting iframe)

    I prefer using first method, but how to create video ends callback? Thanks.