Play sounds sequentially on google chrome android (considering new restrictions on sound reproduction)

I have a small application that plays sequential sounds (a learning application that plays on the soundtracks of a word)

This can be achieved by triggering an event immediately after each sound playback. Sort of:

var sounds = new Array(new Audio("1.mp3"), new Audio("2.mp3")); var i = -1; playSnd(); function playSnd() { i++; if (i == sounds.length) return; sounds[i].addEventListener('ended', playSnd); sounds[i].play(); } 

(a source)

However, now Android Chrome has introduced some new restrictions on sound reproduction: sound events must be fired by the user.

So, when I run a code very similar to the above, the first sound is played, and then I get

 Uncaught (in promise) DOMException: play() can only be initiated by a user gesture. 

How does a sequence of sounds detected at runtime play on Android Chrome?

+5
source share
1 answer

To begin with, Google Chrome on Android has a restriction that prohibits applications from playing HTML audio (s) without explicit user action. However, this is different from how the browser handles it in most cases.

The reason, according to Chromium Org , is that autoplay is not performed on the android, as it will cost the use of data.

Here you can find more information about the same here .

Besides the fact that this leads to a decrease in bandwidth, it also makes sense, since mobile devices are used publicly and in homes where unsolicited sound from random websites can be a nuisance .

However, in later versions this idea was redone, and Chrome on Android began to allow auto-play of audio and video HTML on it. Again, after a series of reviews and discussions, this function was returned to what it was, which makes it mandatory for the user to call HTML audio and video in Chrome for Android.

Here it is what I found more on the same. As the saying goes, the reason was that "we are going to collect some data on how users react to auto-play the video to decide whether this restriction should be respected." And therefore, the option to play without user action is back.

You can also learn more about blocking _autoplay audio (s) and video (s) here on Forbes and Top .

However, this is what I can offer you to try, which will help you achieve what you intend. All you have to do is copy this code and paste it into Chrome for Android. This will help you reset the flag, which is set to by default, which does not allow playing audio and video in HTML format without user intervention :

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

OR

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

If the above procedure does not help / work for you, you can do this:

Go to chrome://flags OR about:flags (this will take you to chrome://flags ) and Enable the "Disable gesture for media playback" option (which is actually the same as the above URL).

+5
source

Source: https://habr.com/ru/post/1273150/


All Articles