我正在开发一个渐进式网络应用程序,主要用于桌面和 Android 上播放音乐。在 PC 上,一切正常。在 Android 上,它突然停止工作。经过一番研究我发现this有关 Chrome 中自动播放政策更改的更新。
要点是,您不能再在没有用户交互的情况下播放媒体。
在此更新之前,我创建了 AudioContext onload
然后就可以使用了。这不再起作用了。
更新帖子指出:
An AudioContext must be created or resumed after the document received a user gesture to enable audio playback
所以我对 .suspend()
做了一些调整和.resume()
相应的上下文onPlay
和onPause
我的应用程序。
在 PC 上这仍然有效,但在 Android 上则不行。我错过了什么吗?
编辑 1:
我找到了this网站声明如下:
Under the new policy media content will be allowed to autoplay under the following conditions:
a) The content is muted, or does not include any audio (video only)
b) The user tapped or clicked somewhere on the site during the browsing session
c) On mobile, if the site has been added to the Home Screenby the user
d) On desktop, if the user has frequently played media on the site, according to the Media Engagement Index
如果其中一个匹配,我知道这是真的。就我而言,b) 和 c) 都得到满足。 所以我想这应该有效吗?
最佳答案
您需要在用户操作事件(例如按钮单击)的音频上下文上调用resume()。我猜您的问题是您调用 context.resume() 的位置不在用户交互事件中。您不能只从 onLoad 调用它。
示例代码:
// Existing code unchanged.
window.onload = function() {
var context = new AudioContext();
// Setup all nodes
...
}
// One-liner to resume playback when user interacted with the page.
document.querySelector('button').addEventListener('click', function() {
context.resume().then(() => {
console.log('Playback resumed successfully');
});
});
文档在这里描述:https://developers.google.com/web/updates/2017/09/autoplay-policy-changes#webaudio
关于javascript - 网络自动播放策略更改恢复上下文不会取消音频静音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50218162/