Reputation: 33
I am creating a chrome extension that automatically pauses the youtube video when I switch to another tab if the tab I am switching to is also youtube playing another video. I am looking for ways to have at least play/pause control over the native youtube player. All I could find on the internet after desperately searching is related to youtube iframe or embedding, like this one.
Is there any way I can achieve this by having direct control over the youtube player object or some other html5 browser features with javascript?
Regards.
Upvotes: 2
Views: 2161
Reputation: 3469
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();
}
Upvotes: 4