YouTube自动播放解决方法? [英] YouTube autoplay workaround?

查看:152
本文介绍了YouTube自动播放解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解自动播放的新变化以及如何将视频静音.但是我有两个问题可以尝试解决此功能.由于我正在工作的网站使用特定的音乐视频来满足审美要求,并且Chrome浏览器是使用最多的浏览器之一,因此我的网站可以在其中正常运行.

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. 阅读开发人员更新后,我看到一条注释,上面写着当"Top Frames委托自动播放权限时,可以允许自动播放".然后链接到向网站添加"allow ="autoplay".我很想知道它是如何工作的,以及如何将其添加到我的网站中.

  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.

如果这样不起作用,我总是可以将一个充满视频的文件夹制作成一个文件夹,并具有JavaScript函数,以随机选择这些视频.我的问题是,由于这项政策,Chrome浏览器也不会自动播放这些视频吗?还是只播放普通视频?

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?

推荐答案

好的,所以我找到了解决此问题的几种方法.allow ="autoplay"的工作方式是,您需要先进行某种形式的网站交互才能运行,然后在每次刷新后它将自动播放.那你该怎么办?

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. 播放/取消静音按钮.当然,这是解决问题的最耗时的方法,因为您需要将其连接到youtube的api.

  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.

发出警报.警报构成站点交互,因此一旦用户单击确定",脚本将正常运行.当然,如果您不希望它每次刷新时都向用户发出警报,那么一个简单的localhost脚本应该可以工作.

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. 告诉您的用户如果正在运行chrome,请使用"ctrl + r".重新加载页面将使其运行正常.

-

当然,只有在您想自动播放声音时,这才可以.如果您不在乎,只是想要静音的视频,则添加?mute = 1& amp"就可以了.

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.

这篇关于YouTube自动播放解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆