I am developing a Phonegap application that plays audio using the Phonegap Media plugin. When I press the home button or the screen lock button on my Android device (KitKat 4.4), the application goes in the background as it should, but the sound does not turn off because the pause event is not triggered. Only when I resume the application, is there a pause event, and my code, to turn off the sound, finally starts. The resume event also fires right after that.
Reading Phonegap documents on a pause event, there is a note about iOS Quirks:
"In the pause handler, any Cordova API calls or plugins that pass through Objective-C do not work with any interactive calls, such as warnings or console.log (). They are processed only when the application resumes in the next execution loop."
This is very similar to what I see on my Android device, although I am wondering if this is the same problem. I really need a fix or workaround, as it is showstopper so that there is no way to mute when the user pauses the application.
Here is my code to add a pause event listener:
document.addEventListener('deviceready', on_device_ready, false); function on_device_ready() { document.addEventListener('pause', on_pause, false); document.addEventListener('resume', on_resume, false); }
UPDATE: I think I decided , although I am not too happy with the decision. I set the PhoneGap config.xml KeepRunning variable to false. I set true and now it gets a pause event when it is supposed to. I feel that I should be able to stop my application from running in the background, and also start some pause code in advance.
Anyway, here is the config.xml line I'm talking about:
<preference name="KeepRunning" value="true"/>
javascript android cordova
learnworkplay
source share