Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova: Pause event not firing

For some reason the pause event is not firing when it should.

If I press the home button on my phone, this is where the pause event should be fired, which should pause the music. But instead it keeps on playing it.

But when I open my app again it is suddenly paused and then resumed. So it fires both events when I return to the app.

Why is onPause not firing when I leave the app?

I have the following code:

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

function onDeviceReady() {
    document.addEventListener("resume", onResume, false);
    document.addEventListener("pause", onPause, false);
}

function onPause() {
    PauseMusic();
    alert('paused');
}

function onResume() {
    ResumeMusic();
    alert('resumed');
}
like image 909
P.Henderson Avatar asked Sep 13 '14 09:09

P.Henderson


2 Answers

I think I had your exact same problem. I had set the Phonegap config.xml variable KeepRunning to false. I set to true and now it gets the pause event when it is supposed to. Here is the config.xml line I am talking about:

<preference name="KeepRunning" value="true"/>

I suppose it makes sense that if the app is not running then the pause event code won't get run until the app runs again. I guess KeepRunning has higher priority than listening to pause event. It would be interesting to get a proper explanation as to why this is exactly at a lower level.

like image 75
learnworkplay Avatar answered Nov 16 '22 19:11

learnworkplay


Are you testing on iOS? I found this to be "normal" behaviour on iOS, as also the Phonegap documentation points out:

In the pause handler, any calls to the Cordova API or to native plugins that go through Objective-C do not work, along with any interactive calls, such as alerts or console.log(). They are only processed when the app resumes, on the next run loop.

So according to the doc you will never see the alert('paused) when closing the app.

However, my observation is that the code is executed on pause of the app, only the console prints are delayed until the next app restart.

If your PauseMusic() call is not executed, you should probably post more code and see, if there's an issue there.

like image 3
qefzec Avatar answered Nov 16 '22 20:11

qefzec