Youtube video iframe API - How to pause the video when it looses focus?

I'm using the Youtube video iframe API and everything works perfect. There's only one thing left for it to work as i spected: I want to detect when the video focus is out, even if it happends in the same window or browser tab, for it to stop playing, or pause.

i have been trying with jquery:

function onPlayerStateChange(event) {
    if (event.data == YT.PlayerState.PLAYING) {
        player.blur(function(){
            $(this).stopVideo();
        });
    }
}

But it doesn't seems to work. Can somebody help?

Upvotes: 2

Views: 1658

Answers (1)

Araymer
Araymer

Reputation: 1525

Try attaching an on method as such:

$('#parentElement').on('focusout', '.playerSelector', function() { //DO STUFF// });

Upvotes: 1

Related Questions