YouTube Autoplay Workaround?
Solution 1:
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?
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.
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);
}
}
- 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&" will work just fine.
Post a Comment for "YouTube Autoplay Workaround?"