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

0 favourites
  • 8 posts
From the Asset Store
In this template the music plays without looping in your game
  • 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!

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

  • 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.

  • 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.

  • 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.

    #pragma mark UIApplicationDelegate implementation
    
    /**
     * This is main kick off after the app inits, the views and Settings are setup here. (preferred - iOS4 and up)
     */
    [ul]
    	[li](BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions[/li]
    [/ul]{
        CGRect screenBounds = [[UIScreen mainScreen] bounds];
    	application.idleTimerDisabled = YES;[/code:iuhfr04t]
  • 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!

  • Next beta? Fantastic!

    Cheers Ashley!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)