Software Archive
Read-only legacy content
17061 Discussions

iOS app audio keeps playing while in background

Jonathan_B_7
Beginner
440 Views

Hello,

I have created a game for iOS using construct 2 and intel xdk,

I want my game to look as native as possible on a iOS environment, so I have been benchmarking my video game against popular video games in the appstore, and to be honest so far it is going great except for a couple of things that I am about to mention.

I checked that whenever you are playing any video game on an iPhone and there is a notification popping up in the screen the game pauses (like a low battery notification for example, or a phone call). Also when the notification center or the control center are displayed (dragged up or down) the game also pauses (check image below - notification center on the left, control center on the right)

notification center IOSI have found a way of making my game pause whenever any of these mentioned actions occur, by using the following code in the index.html file inside Intel XDK:

// register these after deviceready event fires        
document.addEventListener("resign", onResign, false);
document.addEventListener("active", onActive, false);

function onResign() {
    console.log("on resign");
}

function onActive() {
    console.log("on active");
}

(code taken from: https://issues.apache.org/jira/browse/CB-115?jql=project%20%3D%20CB%20AND%20text%20~%20pause)

The code does its job, but despite pausing the game the audio keeps playing in the background (only for the mentioned type of notifications) // when I exit the game for good the audio does stop - this just happens for the mentioned "small pauses",

Is there a way of pausing the audio also when receiving this type of notifications while using the app?

Also, now that I have introduced the mentioned code - my game behaves differently when I resume it after a while / before using the mentioned code I could leave the game in the background for hours and then return to it and the game was going to be exactly where I left it. Now with the new code, when I return to the game after minutes of being idle I see the game where I left it but for like 3 seconds only and then the game restarts. Is there a relation between the code used and this bug? I believe there is because that was not happening before using it,

I am using an iPhone 6S as a test device and Intel 3400, 

Many thanks in advance for your help,

BR,

Jonathan

0 Kudos
5 Replies
Jonathan_B_7
Beginner
440 Views

UPDATE

I created a plugin and added it to the build:

<?xml version="1.0" encoding="UTF-8"?>

-<plugin version="0.1" id="UIBGmode-plugin" xmlns="http://apache.org/cordova/ns/plugins/1.0">

<name>background mode off </name>

 

-<engines>

<engine version=">=3.0.0" name="cordova"/>

</engines>

 

-<platform name="ios">

-<config-file parent="UIBackgroundModes" target="*-Info.plist">

<false/>

</config-file>

</platform>

</plugin>

With this I tried to disable the background mode in the app in order to see if the audio was stopping, but it did not work.

Also I do not think it is a good idea to use this for this particular case because I have been reading in some forums that Apple rejects apps that use the UIBackgroundModes in their code without actually needing it,

From what I have been reading around, the AVPlayer might be responsible for stopping / pausing the audio in the app, but I have no clue on how to use it,

Thanks,
Regards,

0 Kudos
Jonathan_B_7
Beginner
440 Views

Can I get a hand here please?

Still trying to figure this out,

Thanks,

0 Kudos
PaulF_IntelCorp
Employee
440 Views

The behavior you are getting is a function of the Construct2 JavaScript game engine, it is not something that the XDK can manipulate or control, it is something you have to do by modifying the code that is interacting with the Construct2 system.

Note that the XDK is simply building a standard Cordova hybrid app for you (same as PhoneGap). There is no special sauce being applied or special features being added by the XDK, it is simply a convenient way to debug and build those apps. So the behavior of your app is dictated by the way your app (and the Construct2 game engine) execute their JavaScript and respond to events. The questions you are asking will likely be better answered on the Construct2 forum, since we have no internal knowledge about the Construct2 game engine and JavaScript library.

0 Kudos
Jonathan_B_7
Beginner
440 Views

Many thanks for your reply Paul F, 

You were right, the issue was inside Construct - and I am posting below the solution I found in case someone else needs it in the future:

Add browser behaviour to your game:

"Browser (on suspended) = Sound tag (Pause)"
"Browser (On resumed) = Sound tag (resume)"

By doing this I solved the issue of pausing the sound of the game while notifications pop,

However - I am still experiencing the crash issue in my app / and I believe that does have something to do with the cordova plugins I am using, and Intel XDK,

I have tried pretty much everything so far, I have removed all of the plugins in my build (except for the device plugin) and nothing seems to work / my app keeps crashing when I use the phone again after being idle for a few minutes. Especially when I use it again after being locked (as in screenlock),

Do you have any clue of why this might be happening? // maybe there is some line of code I am missing in my HTML or JS files, or a plugin out there that prevents the crash from happening.

If you give me an UDID I can send you an .ipa file so you can test it over there in your offices,

Many thanks in advance,

BR,

0 Kudos
PaulF_IntelCorp
Employee
440 Views

Hi Jonathan -- very glad to see you figured out how to fix the audio behavior problem.

We don't have the resources to debug individual apps, that is why we provide the Debug tab, so you can do this yourself. If you are not seeing this behavior when running your app using the Debug tab, you can, in fact, debug a built app, using a USB connection and remote CDT. Here's a link that provides some background on how to do that > https://software.intel.com/en-us/xdk/docs/intel-xdk-debug-and-test-overview#RemoteChromeDevTools < with your Android device running your app. Unfortunately, if your app is only doing this on iOS devices, not Android, that won't help you much. In that case, if you have a MAC, you can use these instructions to do the same thing using Safari on your Mac > https://software.intel.com/en-us/xdk/articles/debugging-xdk-cordova-apps-built-for-ios-using-mac-and-safari <

0 Kudos
Reply