I had to experiment with Chrome playing sounds. It turned out that even after a user gesture (such as click) it waits for 1000ms and if no sound was played it throws the exception above. In my case the problem was coming from asynchronous track URL loading.
But it also turned out that after the first track is played chrome doesn't care anymore for this 1000ms delay and you can call play programmatically with any timeout you want.
So the solution was to play a micro almost zero-second long muted sound from static resources after the very first time a user clicks on a track and after that load a desired track URL.
首先要了解, 在移动端播放音频视频必须是经过用户的操作才能执行的, 然后下面正式解决问题
引用外国友人(andreybavt)的原话:
大概意思就是
如果异步操作1000毫秒内没有收到任何信息, 就会抛出异常(如下), 浏览器就不会把这个操作认为是来自用户的操作, 所以导致无法播放
, 但有趣的是只要你的音频播放完一次后刚刚所说的1000毫秒规则
就不会重复生效, 所以现在我的解决办法是在用户第一次点击播放的时候先播放一次没有声音且接近0秒的音频(当然你也可以播个几十秒=.=)
, 然后就可以愉快的异步了.详细文章可以参考: 原文传送门
注册canplay事件,必要的话还需要加上一个autoplay属性。