Zenithvox
Zenithvox

Reputation: 93

YouTube autoplay workaround?

I understand the new changes to autoplay and how the video needs to be muted. But I have two questions to try and work around this feature. Since a site I'm working on uses specific music videos to cater to an aesthetic, and since Chrome is one of the most used browsers, it would be nice for my site to function on it.

  1. After reading the developer update I saw a note saying that "Autoplay can be allowed when "Top Frames delegate autoplay permission". Then it links to adding "allow="autoplay" to the site. I'm curious to know how it works and how to hopefully add it to my site.

  2. If that doesn't work I can always make a folder full of videos and have a JavaScript function randomly select those videos. My question is, would Chrome not autoplay those videos as well due to this policy? Or would it only be regular videos?

Upvotes: 3

Views: 2391

Answers (1)

Zenithvox
Zenithvox

Reputation: 93

Okay so I found a few ways around this. The way allow="autoplay" works is that you need to have some sort of website interaction before it being able to run, then on every refresh after that it will autoplay. So what can you do?

  1. Make a play / unmute button. Of course this is the most time consuming way to go around things, as you would need to connect it to youtube's api.

  2. Make an alert. Alerts pose as a site interaction, so once the user clicks "okay" the script will run normally. Of course if you don't want it to alert the user every time there's a refresh, so a simple localhost script should work.

    if (localStorage.getItem("hasCodeRunBefore") === null) {
        var chrome   = navigator.userAgent.indexOf('Chrome') > -1;
   	if (chrome) alert('Click ok to load');
	window.onload = function () {
        localStorage.setItem("hasCodeRunBefore", true);
    }
}

  1. Tell your users to use "ctrl+r" if they are running chrome. As reloading the page will have it run just fine.

--

Of course this is only if you want autoplay with sound. If you don't care and just want a muted video, adding "?mute=1&amp" will work just fine.

Upvotes: 2

Related Questions