Why audio not playing on mobile browser

南笙酒味 提交于 2019-12-23 18:21:27

问题


I have been searching all day. I know that autoplay does not work on mobile and that makes sense. But i want to know why is this not working. Is there a work around for this. The below code works perfectly on desktop but not on mobile.

var audio = new Audio('sound.mp3');
audio.play();

回答1:


Very old question but I ran into it a few times on my journey to the solution to my problem which was that audio seemed to work perfectly fine on desktop but not at all on select mobile browsers.

My problem was that in a touchevent I was doing e.preventDefault() which (somehow?) made the event 'not trusted' meaning everything looked fine but no audio would play.

I fixed it by just not using the touch event and relying on the click event which is fired after and setting touch-action: manipulation in the css. Not the best solution, but hey. Pretty silly the event becomes non-trusted on a tap on a control to play the sound with a prevent default so that it won't zoom in on a double tap.

Hopefully this helps someone with a similar problem.

(from comment from Manuel Graf on this question)



来源:https://stackoverflow.com/questions/46345883/why-audio-not-playing-on-mobile-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!