简体   繁体   English

如何在没有用户交互的情况下下载和播放音频剪辑?

[英]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.

相关问题 如何使用angularjs播放多个音频剪辑? 我可以从HTML中选择文件吗? - How can I play multiple audio clips with angularjs? Can I pick the file from my html? 如何排队一系列声音HTML5 <audio> 声音片段按顺序播放? - How can I queue a series of sound HTML5 <audio> sound clips to play in sequence? 如何只加载波形并等待用户点击“播放”以下载Wavesurfer-js上的音频? - How can I load only waveform and wait to user click 'play' to download the audio on Wavesurfer-js? 需要用户交互才能播放音频解决方法? - User interaction needed to play audio workaround? 如何在不使用的情况下在JavaScript中播放声音 <audio> 标签? - How can I play sound in JavaScript, without using the <audio> tag? 我可以在移动设备上一页上的用户交互中自动播放音频吗? - Can I autoplay audio from a user interaction on a previous page on mobile? 如何在不使用相同音频 ID 的情况下播放从数据库中获取的多个音频文件 - How can i play multiple audio file gotten from a database without using same audio id 如何在php中播放音频剪辑? - How can i play an audio clip in php? 如何将 arrayBuffer 作为音频文件播放? - How can I play an arrayBuffer as an audio file? 如何在后台播放音频但延迟? - How can I play audio in the background but delayed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM