[英]How can I download and play audio clips without user interaction?
Is there a way programmatically to begin audio play before the user has interacted with the page?有没有办法在用户与页面交互之前以编程方式开始音频播放?
With the following JavaScript:使用以下 JavaScript:
console.log('Playing abc.');
XYZ.abc = new Audio('audio/abc.wav');
XYZ.abc.play();
I'm getting an error message,我收到一条错误消息,
Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.
Uncaught (in promise) DOMException: play() 失败,因为用户没有先与文档交互。
When I click on the page, that goes away.当我点击页面时,它消失了。
Is the standard UI pattern to request the user to click to start sound, or is there a loophole to do this programmatically?是标准的 UI 模式要求用户点击开始声音,还是有漏洞以编程方式执行此操作?
Thanks,谢谢,
So what the problem is that users don't usually expect audio to come from the website, so chrome requires your audio or video element to be muted by default.那么问题是用户通常不希望音频来自网站,所以 chrome 要求您的音频或视频元素默认静音。 For the
<audio>
tag, you need to add the muted attribute, so -> <audio muted>
对于
<audio>
标签,需要添加 muted 属性,所以 -> <audio muted>
Respectively, the js element will have a .muted property.相应地,js 元素将有一个 .muted 属性。 So, what you need is
XYZ.abc.muted = true;
所以,你需要的是
XYZ.abc.muted = true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.