Rajan
Rajan

Reputation: 426

HTML5 audio tag not playing sound for the very first of unfocused tab in chrome and 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.

Upvotes: 1

Views: 7789

Answers (1)

Xeos
Xeos

Reputation: 6487

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

Upvotes: 7

Related Questions