It's really annoying how I have looping background music but whenever I press the home button or any other button but the back button, the audio still plays. Is there anyway around this?
We should add a proper event listener you could subscribe on Sup.Input.on(...).
For now, you can work around like this:
declare let document; function handleVisibilityChange() { if (document.hidden) music.pause(); else music.play(); } document.addEventListener("webkitvisibilitychange", handleVisibilityChange, false);
The first line should outside anything.
The remaining can be outisde anything if you can access to the music or in a awake function.
In any case, it must not be in an update. Otherwise, you would subscribe to the event too many times.
Also, when you get some errors, make you sure to show them. It will help find the solution to the problem ;)
It still doesn't work, this is my code
declare let document;
let inGameMusicPlayer = new Sup.Audio.SoundPlayer("Sound", 1.0, { loop: true });
class MouseCoordsBehavior extends Sup.Behavior {
update() {
// Mouse coordinates returned by .getMousePosition() are normalized to (-1, 1)
let { x, y } = Sup.Input.getMousePosition();
x = (x + 1) / 2 * Sup.Input.getScreenSize().x;
y = (1 - (y + 1) / 2) * Sup.Input.getScreenSize().y;
this.actor.textRenderer.setText(`X = ${Math.round(x)}\nY = ${Math.round(y)}`);
if (x >= 0 && x <= 170 && y >= 0 && y <= 170 && Sup.Input.wasMouseButtonJustReleased(0)) {
this.actor.textRenderer.setText("OMG BUTTONS");
handleVisibilityChange();
}
}
}
Sup.registerBehavior(MouseCoordsBehavior);
function handleVisibilityChange() {
if (document.hidden) inGameMusicPlayer.pause();
else inGameMusicPlayer.play();
}
document.addEventListener("webkitvisibilitychange", handleVisibilityChange, false);
Do you have Chrome installed on your phone ? Can you try accessing a build directly from the browser instead of the packaged version ?
EDIT: Maybe this page can be of some help ? http://caniuse.com/#feat=pagevisibility
I needed the webkit prefix to make it work on my phone but maybe you don't ?