Bypass Sound Restriction

155 views
Skip to first unread message

Gautam Mehta

unread,
May 28, 2021, 1:37:54 PM5/28/21
to DroidScript
I am working on a game in webview. The game has a background music which should start playing automatically as the page loads. But I have learnt that the user needs to interact with the page at least once to enable sounds on the webpage. How can I bypass this. Any help would be gr8.

Alan Hendry

unread,
May 29, 2021, 10:21:38 AM5/29/21
to DroidScript

Hi,
What if you use javascript to "fake" a user interaction, like
document.getElementById("myCheck").click();
(You'd need a link/button with id="mycheck" with an onclick event that didn't do very much). 
Regards, ah

Gautam Mehta

unread,
May 29, 2021, 12:45:48 PM5/29/21
to DroidScript
Yeah that's what I had thought.:-)

And btw, I think the document.getElement.... method will not work as it was not an actual click but one generated by the site itself. 
But we can use the DS StimulateTouch method I think as it is not an internal business.

But isn't there a cleaner way to do this?

Alan Hendry

unread,
May 29, 2021, 3:02:08 PM5/29/21
to DroidScript
Hi,
In post webView SimulateTouch, Steve said 
"
I have never been able to get SimulateTouch to work with a webview at all
I just have assumed it doesn't work
"
You could try a native app with a webView loading URL of a local file with your HTML, 
Then use web.execute to fake user interaction.
(Or play the music using app.PlaySound).
Regards, ah

Steve Garman

unread,
May 30, 2021, 5:48:45 AM5/30/21
to DroidScript
Without knowing what kind of user interaction is needed to start the music it is hard to guess what might help

There are cases where web.Focus() called when loading has completed might be enough

Do you have some sample code that demonstrates the problem?

Gautam Mehta

unread,
May 31, 2021, 1:06:14 AM5/31/21
to DroidScript
Well I am using phaser 2 as the game engine and the code is a mess but in short its like this:
--preload sound
--create sound
--set it to loop
--play sound

And Yes I tried SimulateTouch in webview local url to activate the sound but it didn't work.

Gautam Mehta

unread,
May 31, 2021, 3:31:34 AM5/31/21
to DroidScript
I found out the actual problem and the solution. It wasn't with webview not allowing sound without user gesture. Apparently it does allow as when I tried it with a smaller sound file it worked. So the issue was with the game engine which was not able to load the sound file instantly and hence it didn't play.
Fixed it with this:

bgSndInterval=setInterval(function() {
        if (bgSnd.isPlaying) {
            clearInterval(bgSndInterval);
        }
        else {
            bgSnd.play();
        }
    },100);

Thanks for your time 🙂

Steve Garman

unread,
May 31, 2021, 3:57:21 AM5/31/21
to DroidScript
Thanks for letting us know what you have discovered

When I have an unexplained problem in one of my apps, I often find that writing a cutdown version to explain the problem to someone else helps me to track down a fix

Gautam Mehta

unread,
Jun 1, 2021, 3:15:25 AM6/1/21
to DroidScript
There we go🤜🏼
Reply all
Reply to author
Forward
0 new messages