你可以在结尾处阅读问题总结。
我需要在我的网站上与声音自动播放,在用户同意后。这里是example (对不起,网站是在意大利语)的地方,它是在滚动上实施,这与cookie和视频自动播放政策,但我希望得到一个协议的点击(用户点击同意按钮)。
重点是在第一次访问时询问用户协议,然后在不要求确认的情况下自动播放视频。在上面的例子中,它几乎是工作的,因为我需要它工作,但我不知道他们是如何做到的,请帮助我理解这个逻辑。
我知道谷歌关于媒体自动播放的政策,所以我需要用户同意后才能自动播放视频,与上面的例子相同。
我有这样的代码:
<video id="video" preload="auto" playsinline controls>
<source src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/mp4">
</video>下面是JS中的代码(使用video.js和jQuery):
var videoPlayer = videojs('video');
videoPlayer.ready(function() {
var promise = videoPlayer.play();
});摘要:
主要目标是弄清楚如何在示例example (对不起,网站是意大利语)上自动播放声音(如何实现它)。它的工作方式是这样的:当用户第一次来到网站时,他会收到通知,告诉用户网站正在使用cookies,然后如果用户滚动或点击网站,那么自动播放声音在当前会话和此域的所有后续会话上都有效。我需要你的帮助来弄清楚如何实现这个逻辑。
发布于 2020-07-13 19:18:08
您可以在用户单击协议按钮后将source标记插入到video标记中。html之前不会被加载,所以你的站点上没有任何Google内容。
$(document).ready(function(){
$("#enableGoogleContentButton").on("click", function(){
$("#video").append('<source src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/mp4">')
});
});<video id="video" preload="auto" playsinline controls>
<!--empty-->
</video>
...
<button id="enableGoogleContentButton"> enable Google Content </button> 这个例子也适用于Vanilla JS (我不确定我是否理解了您的问题,如果我不理解,很抱歉)
编辑
您可以将有关过去点击的信息存储在cookie/localstorage中
$(document).ready(function(){
if(localstorage.cookieaccept=""){
//if there's no cookie set yet
$("#enableGoogleContentButton").on("click", function(){
//test for click
$("#video").append('<source src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/mp4">');
//set the local storage "cookieaccept" to true
localstorage.cookieaccept="true";
$("#enableGoogleContentButton").remove()
//remove the "enable content"-button
});
$("document").on("scroll", function(){
//or scroll
$("#video").append('<source src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/mp4">');
//set the local storage "cookieaccept" to true
localstorage.cookieaccept="true";
$("#enableGoogleContentButton").remove()
//remove the "enable content"-button
});
}
else{
//if cookie is already set, add the video
$("#video").append('<source src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/mp4">')
$("#enableGoogleContentButton").remove()
//remove the "enable content"-button
}
});我希望这能满足你的要求。
发布于 2020-07-14 00:03:22
我认为这是不可能的。网站必须先通过MEI。
https://developers.google.com/web/updates/2017/09/autoplay-policy-changes#mei
https://stackoverflow.com/questions/62873947
复制相似问题