How do I use the Youtube API?

Asked

Viewed 169 times

0

I have a page with a video that opens in a modal, I need it to be paused when the modal is hidden. I tried examples I found on other sites but it didn’t work.

I need to add some extra file to use the API control?

  • How are you embedding the video ?

1 answer

1


I don’t know how you’re closing your modal, but if you’re using .hide() jQuery, you can do the following:

function pause_video(){
    $('#youtube_iframe_id')[0].contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
}

function hideCallBack() { 
    pause_video()
}

With the two functions added to your code, your video will pause when you close the modal using:

$('#modal_id').hide(hideCallBack);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.