2

a colleague of me made a crossplattform game app with Cordova. now he is in vacation and I have the problem, that the background music of the app is still playing, if you press the home button and leave the app. Also if you make a phone call.

I tried to set the Android <preference name="KeepRunning" value="false"/>and also to true, but that didn't help.

Any idea what to do, to handle this problem?

DeeFour
  • 436
  • 5
  • 17

3 Answers3

3

I found an anwser by myself:

we had to set <preference name="KeepRunning" value="true"/> and then add this events:

function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
}

// device APIs are available
//
function onDeviceReady() {
    document.addEventListener("pause", onPause, false);
    document.addEventListener("resume", onResume, false);
}

function onPause() {
    // Handle the pause event
    // Set loudness to zero
}

function onResume() {
    // Handle the resume event
    // Set loudness to one

}

If KeepRunning is set to false, Cordova does not recognize the onPause/onResume events.

DeeFour
  • 436
  • 5
  • 17
2

My problem was, that the onPause event war fired AFTER the application resumes. -.- But I could figure out a small hack which works. At least for me:

document.addEventListener("visibilitychange", function() {
        if (document.hidden) {
            //onPause stuff
        } else {
            //onResume stuff
        }

    }, false);

This works for me on Android when the game minimized, the device turns off, or the tab is switched.

SleepingMole
  • 459
  • 1
  • 4
  • 11
1

Use the cordova pause event:

document.addEventListener('pause', function() {
  // Tell your audio library to pause here
});

When the user re-opens the app, the resume event will fire:

document.addEventListener('resume', function() {
  // Tell your audio library to start playing again
});
Tom Jardine-McNamara
  • 2,418
  • 13
  • 24