问题描述
我们如何在移动浏览器上播放音频,而不是在桌面浏览器上播放,而在移动浏览器上播放则是针对特定问题的解决方案.代码是var audio = new Audio('sound.mp4')audio.play()
how can we play audio on mobile broswer is playing on desktop browser but not playing on mobile what is the solution for the particular problem..code isvar audio=new Audio('sound.mp4')audio.play()
推荐答案
Mobile Safari和其他可能仅允许响应用户操作播放音频.因此element.addEventListener( 'click', () => audio.play() )
将起作用,但autoplay
属性或document.addEventListener('DOMContentLoaded', () => audio.play() )
将不起作用.
Mobile Safari, and probably others, only allow audio to play in response to a user action. So element.addEventListener( 'click', () => audio.play() )
will work, but the autoplay
attribute or document.addEventListener('DOMContentLoaded', () => audio.play() )
won't.
这篇关于在移动浏览器上通过html音频标签播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!