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="http://www.youtube.com/player_api"></script>

    <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) {
            event.target.playVideo();
        }

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

    </script>


Hope it helps!