Music continues when app is in background in android

Get help using Construct 2

Post » Wed Jun 15, 2016 12:35 pm

Hi,

when I export my game to android and send the app to the background the sound and music continues playing. It even continues playing when turning of the screen of the device.

I exported the game with cordova (no media plugin or crosswalk).
The "Play in Background" property of the Audio object is activated and works fine in the browser.

How do I stop the sound in the exported version?
B
7
S
1
G
1
Posts: 10
Reputation: 427

Post » Wed Jun 15, 2016 1:03 pm

Probably related:
The Browser Objects "On Suspended" and "On Resumed" events also don't trigger when sending the app to the background.

And I had the same problem when using Intel XDK before.
B
7
S
1
G
1
Posts: 10
Reputation: 427

Post » Wed Jun 15, 2016 2:36 pm

Okay, I could solve the problem on my own, after discovering this in another topic:
Ashley wrote:Closing as won't fix: the problem is with the browser, not C2.

In Chrome for iOS, it correctly triggers 'On suspend' and 'On resume' when switching tabs, but not when returning to the home screen and going back (which Safari does correctly). Old Android stock browsers simply don't support the Page Visibility API which is the way the browser tells C2 if the page has been hidden, so it can't be supported. It should work in Chrome for Android though.



I solved the problem by adding this code snippet to the exported project:

Code: Select all
document.addEventListener("deviceready", function() {   
    document.addEventListener("pause", simulatePageVisibilityApiHide, false);
    document.addEventListener("resume", simulatePageVisibilityApiUnhide, false);
}, false);

function simulatePageVisibilityApiHide() {
    document.hidden = true;
    document.mozHidden = true;
    document.webkitHidden = true;
    document.msHidden = true;
    var event = new Event('visibilitychange');
    document.dispatchEvent(event);
}

function simulatePageVisibilityApiUnhide() {
    document.hidden = false;
    document.mozHidden = false;
    document.webkitHidden = false;
    document.msHidden = false;
    var event = new Event('visibilitychange');
    document.dispatchEvent(event);
}
B
7
S
1
G
1
Posts: 10
Reputation: 427


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 5 guests