Music continues when app is in background in android

0 favourites
  • 3 posts
From the Asset Store
Create your own casino app and import it to Google Play and the App Store.
  • 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?

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Okay, I could solve the problem on my own, after discovering this in another topic:

    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:

    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);
    }[/code:1n2qqp0m]
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)