Youtube video runtime

3

I've been searching and I did not find anything concrete, I know that there is the youtube API but I did not find any right source.

How can I check on php whether or not the user watched a youtube video embedded in my site? Or even count how many minutes the user watched?

    
asked by anonymous 25.01.2016 / 10:27

1 answer

3

I was able to solve using the google API, which shows the time if the user gives play, pause or if the video ends:

<script type="text/javascript" src="https://www.youtube.com/iframe_api"></script><scripttype="text/javascript">
var player;
function onYouTubeIframeAPIReady() {
    player = new YT.Player( 'player', {
        events: { 'onStateChange': onPlayerStateChange }
    });
}

function onPlayerStateChange(event) {
    switch(event.data) {
        case 0:
            alert('vídeo acabou');
            break;
        case 1:
            alert('começou em '+player.getCurrentTime());
            break;
        case 2:
            alert('pausado em '+player.getCurrentTime());
            break;
    }
}
</script>

And just pass the url inside an iframe:

<iframe id="player" src="https://www.youtube.com/embed/DjB1OvEYMhY?enablejsapi=1"></iframe>
    
25.01.2016 / 12:22