HTML5音频标签在chrome和safari中的非重点标签的第一个不播放声音吗? [英] HTML5 audio tag not playing sound for the very first of unfocused tab in chrome and safari?

查看:66
本文介绍了HTML5音频标签在chrome和safari中的非重点标签的第一个不播放声音吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当浏览器中的选项卡可用时,我都想在应用程序中播放声音,而不必关注焦点.我正在使用HTML 5音频标签播放声音.但是chrome和safari会延迟音频播放,直到用户导航或聚焦到该选项卡为止.整个音频播放声音请求都在标签页焦点时播放.当您一次听到很多声音播放时,它变得太嘈杂.是否可以在没有浏览器延迟的情况下从未聚焦的选项卡播放声音?向我建议任何可用于处理这种情况的替代解决方案.提前致谢.

I want to play a sound in my application whenever a tab is available in browser irrespective focus. I am using HTML 5 audio tag to play the sound. But chrome and safari defer the audio playing until the user navigate or focus to the tab. The entire audio request to play sound is played at a time of tab focus. It becomes too noisy when you hear lot of sound playing at a time. Is it possible to play a sound from unfocused tab without browser defer nature? Suggest me any alternative solution available for handling this situation. Thanks in advance.

推荐答案

自Chrome 46以来,这是预期的行为.为了优化浏览器性能并节省功耗,Google禁用了背景标签上的自动播放功能.但是,从Chrome 47开始,人们可以通过启用 Chrome标记

This is the expected behavior since Chrome 46. To optimize browser performance and to conserve power, Google has disabled the auto-play on the background tabs. However starting from Chrome 47, there is a way for a person to override it by enabling the Chrome flag

chrome://flags/#disable-gesture-requirement-for-media-playback

更新:自Chrome 60起,Chrome标志不再可用(感谢@gordie)

UPDATE: Chrome flag is no longer available since Chrome 60 (thank you @gordie)

没有解决方法允许声音在背景"选项卡中播放.解决方案是在页面聚焦之前不播放任何声音.为此,您可以使用window.onfocus事件处理程序,也可以使用Visibility API(更多信息).

There is no workaround that would allow sound to play in the 'background' tab. The solution is to not play any sounds until the page is focused. For that you can either use the window.onfocus event handler or, preferably, the Visibility API (more info).

这篇关于HTML5音频标签在chrome和safari中的非重点标签的第一个不播放声音吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆