Is there a way to disable music/sound when putting the phone on standby?

Currently, the sound stays active while the phone is on standby. (As long as the browser is still active while turning it off.)

Currently happening only on android devices.

Hi @Helldubs,

You can use the Visibility API available in most browsers to detect when the display is turned off or the tab loses focus and disable the sounds.

Thanks Leonidas. Is there an easy way to get a reference to the games html document in script?

I know there is app.assets.get(), but what ID do I put there to reference the main html doc?

Sure, all Javascript no matter where it’s written initially ends up appended to the main document.

So anywhere in your code you can do the follow, and it will be valid:

var VisibilityApi = pc.createScript('visibilityApi');

// initialize code called once per entity
VisibilityApi.prototype.initialize = function() {
    
    document.addEventListener(
      "visibilitychange",
      this.handleVisibilityChange.bind(this),
      false
    );   
};

VisibilityApi.prototype.handleVisibilityChange = function(){

    if (document.hidden) {
        console.log('hidden');
    }else{
        console.log('visible');
    }
};
1 Like

Thanks!

I implemented it like this:

  if (document.visibilityState === 'visible') {
                 console.log("tabbed in");
             } else {
                 console.log("tabbed out");
             }

but it doesn’t show up in the console when I switch between tabs. Shouldn’t it do that though?

I’ve updated the code above with a full implementation.

Ah, the problem was in your original code you had
document.addEventListener(visibilityChange, function(){}),

whereas
document.addEventListener(“visibilitychange”, function(){}),

would be correct. This is why it didn’t work at first for me.

Thanks for your help, it’s working now!

1 Like