Browse Source

Mute sounds on mobile when focus is lost

1489-mobile-unfocus-mute
Vildravn 3 years ago
parent
commit
aa08a8e261
1 changed files with 18 additions and 7 deletions
  1. +18
    -7
      src/client/js/main.js

+ 18
- 7
src/client/js/main.js View File

@@ -30,6 +30,8 @@ define([
return () => requestAnimationFrame(updateMethod);
};

let restoreSoundOnFocus = false;

return {
hasFocus: true,

@@ -61,8 +63,8 @@ define([
},

start: function () {
window.onfocus = this.onFocus.bind(this, true);
window.onblur = this.onFocus.bind(this, false);
window.addEventListener('focus', this.onFocus.bind(this, true));
window.addEventListener('blur', this.onFocus.bind(this, false));

$(window).on('contextmenu', this.onContextMenu.bind(this));

@@ -82,11 +84,20 @@ define([
$('.loader-container').remove();
},

onFocus: function (hasFocus) {
//Hack: Later we might want to make it not render when out of focus
this.hasFocus = true;
onFocus: function (isFocussed) {
this.hasFocus = isFocussed;

if (window.isMobile) {
if (!this.hasFocus) {
restoreSoundOnFocus = !sound.muted;
sound.onToggleAudio(false);
} else if (restoreSoundOnFocus) {
sound.onToggleAudio(true);
restoreSoundOnFocus = false;
}
}

if (!hasFocus)
if (!isFocussed)
input.resetKeys();
},

@@ -118,4 +129,4 @@ define([
fnQueueTick();
}
};
});
});

Loading…
Cancel
Save