最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML5 audio tag not playing sound for the very first of unfocused tab in chrome and safari? - Stack Overflow

programmeradmin1浏览0评论

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 bees 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.

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 bees 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.

Share Improve this question asked Apr 12, 2016 at 2:20 RajanRajan 4262 gold badges7 silver badges28 bronze badges 1
  • So all you want is a audio tag to play even if you're on another tab? – zer00ne Commented Apr 23, 2016 at 0:10
Add a ment  | 

1 Answer 1

Reset to default 7

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

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

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).

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论