🤑 Indie game store🙌 Free games😂 Fun games😨 Horror games
👷 Game development🎨 Assets📚 Comics
🎉 Sales🎁 Bundles

Stop audio playing when app is suspended (Android)

A topic by NinjaDJ created 1 year ago Views: 608 Replies: 8
Viewing posts 1 to 2

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?

Moderator(+2)

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);

Where should I add this? Before the update function or outisde?

Deleted post
Moderator

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);
Moderator

Does it work if you test on a browser on your PC ? It should work when you switch tab.

What browser do you use on your phone ?

Do you still have any error ? If yes, can you post them ?

(Edited 1 time)

It doesn't work when I switch tab, I'm trying to run it as a native Android app using Intel XDK to build it

And nope, no errors. Code compiles perfectly fine

Moderator (Edited 1 time)

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 ?

It seems to work on Chrome, is there a reason why it doesn't work on Firefox or on the packaged version?

Moderator

For the packaged version, it depends on which version of Chrome is packaged along. For firefox, it may work if you remove the "webkit" prefix ?

(Edited 1 time) (+1)

Would building it with Crosswalk solve this problem?

Edit: Bundling the app with Crosswalk seems to make this work! Thanks a bunch!

Deleted post