在某些移动浏览器上无法通过javascript播放音频
我正在尝试做一件简单的事情.我希望在用户进入网页后10秒钟后播放一些音频.我使用了以下代码
I'm trying to do a simple thing. I want some audio to play exactly after 10 seconds when the user enters the webpage. I used the following code
var aud=new Audio("someAudio.mp3");
$(document).ready(function(){
setTimeout(function(){aud.play()}, 10000);
});
它在桌面浏览器上运行良好.但是,尽管可以在Firefox中运行,但某些音频无法在诸如Google Chrome之类的移动浏览器中播放.可能的原因是什么?如何解决?我看到了一些类似的问题,但没有找到合适的答案. 预先感谢
It is working perfectly fine on desktop browsers. However, the audio is not playing in some mobile browsers like Google Chrome though it is working in Firefox. What may be the possible reason for this and how to fix it? I saw some similar questions but didn't find a suitable answer. Thanks in advance
我正在尝试做一件简单的事情.我希望在用户进入网页后10秒后播放一些音频.
I'm trying to do a simple thing. I want some audio to play exactly after 10 seconds when the user enters the webpage.
除非有用户交互,否则您将无法.
You can't unless there has been user interaction.
处理某些元素的click
事件.在该事件处理程序中,播放其他音频. (该音频可以是无声的!)从加载开始经过10秒钟后,如果用户触摸/单击了某项,并且您已经完成此操作,则应该可以播放音频文件.
Handle a click
event for some element. In that event handler, play some other audio. (This audio can be silent!) After 10 seconds have passed from load, if the user has touched/clicked something, and you've done this, you should be able to play your audio file.