用谷歌浏览器播放audioSource的坑

    xiaoxiao2025-08-07  22

    问题描述

    在cocos creator中测试cc.AudioSource功能的时候,发现了一个特别奇怪的现象

    从谷歌浏览器中运行时,怎么都听不到声音,然后F12查看发现有这样一个警告

    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. https://goo.gl/7K7WLu

    然后,我用模拟器运行,能正常听到声音

    后来我又用QQ浏览器运行试了一下,也能正常听到声音,但是也有个警告

    The Web Audio autoplay policy will be re-enabled in Chrome 71 (December 2018). Please check that your website is compatible with it. https://goo.gl/7K7WLu

    其实,以上两句最终表达的意思就是

    谷歌浏览器从71版本开始就开启了谷歌的安全策略,因此导致的声音不能自动播放,必须在用户有了操作之后才可以播放音乐,比如可以点击一下后边的 https://goo.gl/7K7WLu 这个链接,也是可以触发播放音乐的。

    还有一种方法,就是修改一下谷歌安全策略 在谷歌浏览器上输入 :

    chrome://flags/#autoplay-policy

    会看到如下的一个选项,把它修改为第二项 No user gesture is required

    至此完美解决!

    最新回复(0)