How to Pause a Youtube Player When Hiding the Iframe

Stop embedded youtube iframe?

Unfortunately these API's evolve very fast. As of May 2015, the proposed solutions don't work anymore, as the player object has no stopVideo method.

A reliable solution is to be found in this page (1) and it works with an:

<iframe id="youtube_player" class="yt_player_iframe" width="640" height="360" src="http://www.youtube.com/embed/aHUBlv5_K8Y?enablejsapi=1&version=3&playerapiid=ytplayer"  allowfullscreen="true" allowscriptaccess="always" frameborder="0"></iframe>

and the following JS/jQuery code:

$('.yt_player_iframe').each(function(){
this.contentWindow.postMessage('{"event":"command","func":"stopVideo","args":""}', '*')
});

Youtube Iframe disable pause video

There's no way to disable pausing entirely.

You could listen for YT.PlayerState.PAUSED events in an onStateChange handler and immediately call playVideo() when you detect one, but... I don't know, that sounds like it wouldn't be very user-friendly.

Disable play/pause button of a YouTube video

I disabled it here:
https://codepen.io/polinq/pen/mdyWmMm

iframe{
pointer-events: none; /* Disable any user interaction at all */
}

Is that what you are looking for?

How can I play/pause youtube player on its native site itself using javascript?

This is very simple on youtube video page.

Try this simple js code:

var video = document.querySelector("video");
if (video.paused){video.play();} else {video.pause();}

But for embedded Youtube videos you may want to review through the Youtube JavaScript API Reference docs. Read this for more details: https://stackoverflow.com/a/15165166/1225070

For html5 Videos.

Try this:

var vid = document.getElementById("myVideo");

function playVid() {
vid.play();
}

function pauseVid() {
vid.pause();
}


Related Topics



Leave a reply



Submit