How do I keep music playing on iOS with the screen off?

Get help using Construct 2

Post » Tue Sep 30, 2014 4:01 pm

I'm trying to create a music player on iOS using Phonegap CLI, and Xcode.

All is working fine, except a big problem is that audio is suspended when the the app is out of focus, or the phone enters standby.

Does anyone know how to keep audio playing in the background like a regular audio player?

I have added the 'Required background Modes' field for audio in the .plist file. Other non C2 phonegap users suggest this but it makes no difference.

I have also tried added the Phonegap media plugin but it seems to make no difference.

Any ideas? Please help!
B
24
S
7
G
1
Posts: 186
Reputation: 2,716

Post » Tue Sep 30, 2014 4:11 pm

Not sure you can. MAYBE with some conventional HTML but my experience is that C2 gets suspended as soon as it loses window focus on mobile.
Developing Surolace, the survival role playing space game.

surolace-survival-role-playing-space-game_t116953
B
14
S
4
Posts: 303
Reputation: 1,730

Post » Tue Sep 30, 2014 4:26 pm

Construct 2 does this deliberately because it's designed as a game engine and that's probably what you want a game to do when it goes in to the background or standby. I guess the Audio object should have a 'play in background' option to work around that.
Scirra Founder
B
399
S
236
G
89
Posts: 24,525
Reputation: 195,382

Post » Tue Sep 30, 2014 6:51 pm

A play in background option would be great but I guess that will not be any time soon.

Another approach I was thinking about was to prevent the device from sleeping, which seems to be possible with a phonegap plugin called Insomnia, https://build.phonegap.com/plugins/355

Are there any other ways to prevent the device from sleeping? From memory cocoonjs did this by default on android, but I'm specifically targeting ios.
B
24
S
7
G
1
Posts: 186
Reputation: 2,716

Post » Tue Sep 30, 2014 9:43 pm

I would assume it's the same for iOS as it is for Android. In that case you would need to wait until the wrapper supported this.
B
56
S
15
G
13
Posts: 826
Reputation: 17,675

Post » Wed Oct 01, 2014 9:14 am

OK, I found a hack that works. It stops the iOS device going into standby, and shutting off audio. Not ideal, but it works.

This is for Phonegap CLI with XCode, not for Phonegap Build.

Edit Appdelegate.m in APP_FOLDER\platforms\ios\YOUR_APP_NAME\Classes\

Add the line application.idleTimerDisabled = YES; in the location below. This stops the device from going to standby when the screen is not being touched.

This is also useful for tilt based games where there is no screen contact.

Code: Select all
#pragma mark UIApplicationDelegate implementation

/**
 * This is main kick off after the app inits, the views and Settings are setup here. (preferred - iOS4 and up)
 */
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
   application.idleTimerDisabled = YES;
B
24
S
7
G
1
Posts: 186
Reputation: 2,716

Post » Wed Oct 01, 2014 10:24 am

I was going to put this in to the next beta actually. I really wouldn't hack the code to disable standby, that will have such poor consequences for battery life that nobody will use your app!
Scirra Founder
B
399
S
236
G
89
Posts: 24,525
Reputation: 195,382

Post » Wed Oct 01, 2014 12:16 pm

Next beta? Fantastic!

Cheers Ashley!
B
24
S
7
G
1
Posts: 186
Reputation: 2,716


Return to How do I....?

Who is online

Users browsing this forum: kainrazial and 20 guests