All thoughts and posts are my own, said by @rakibtg.
posted at Tuesday, 6 June 2017

YouTube makes it so simple to listen when a embeded YouTube player finishes playing a video and stops. We can do this by using the very simple player API, interestingly we don't need an API key or token to use this feature.

Here is the code for you,

    <script src=""></script>


        // create youtube player
        var player;
        function onYouTubePlayerAPIReady() {
            player = new YT.Player('player', {
              height: '390',
              width: '640',
              videoId: 'hjdbXdTt48E',
              events: {
                onReady: onPlayerReady,
                onStateChange: onPlayerStateChange

        // autoplay video
        function onPlayerReady(event) {

        // when video ends
        function onPlayerStateChange(event) {        
            if( === 0) {          
                alert('finishes playing');


Hope it helps!