问题描述
我有一个动画项目,需要在所有主要浏览器(包括手机上的浏览器)上自动播放音频.我们还需要控制音频,以使其在暂停并再次播放后继续播放.这意味着我们不能使用iframe
,因为它会每次重播音频.另外,只需发现iframe
现在无法在Chrome中自动播放...是否有解决此问题的解决方法?
I have an animation project that needs to autoplay the audio on all main browsers includes those on mobiles. We also need to control the audio to make it continue playing after it paused and played again. That means we can't use iframe
because it will replay the audio everytime. Plus, just find out that iframe
can't autoplay in Chrome now... Will there be any workaround to fix this problem?
推荐答案
当您尝试自动播放音频而没有用户首先与文档进行交互时,它将引发错误.因此,关键是要使用户首先与文档进行交互.
When you attempt to autoplay audio without the user interacting with the document first, it will throw an error. So the key is to get the user interact with the document first.
您可以做的是,当用户打开页面时,他必须首先单击某个地方或某个按钮以使实际内容可见,然后在单击交互之后播放音频.
What you can do is, when user opens the page, he must first click somewhere or some button to make the actual content visible and then with that play the audio after the click interaction.
有关捕获错误的信息,请参见扩展DOM元素,处理错误并优雅降级.
For catching the error see Extend the DOM Element, Handle the Error, and Degrade Gracefully.
PS:我注意到在切换选项卡,重新加载切换到的选项卡以及切换回+重新加载时,它可以正常工作,而无需任何变通方法.不是解决方案,我只是想知道为什么会发生这种情况(我使用的是最新的Chrome).它必须不执行任何操作即可触发文档交互
PS: I noticed when switching tab, reload the tab you switched to and switch back+reload it just works without any workaround. Not a solution, i just wonder why this happens (i am using latest Chrome). It must trigger document interaction without doing anything
这篇关于是否有任何变通办法(或黑客手段)使音频在Chrome,Firefox和Safari上自动播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!