1

I'm developing an app that switches music using a MPMusicPlayerController.systemMusicPlayer() from the MPMedia framework. I need the song to change anywhere between 30 seconds and 1 minute for up to 100 minutes when the app is in the background. In the foreground of course everything works exactly how it should but in the background I can get the app to do this for about 3 minutes which seems to be the limit imposed on background tasks in iOS8. Is there a way to extend the allowable time that app can operate in the background?

I'm currently using the following code to register the background task where background_task is of type : var background_task: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid

func registerBackgroundTask()
{
    println("Background task registered")
    background_task = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler
    {
        [unowned self] in
        self.endBackgroundTask()

    }
}

and then this ends it:

func endBackgroundTask()
{
    NSLog("Background task ended.")
    UIApplication.sharedApplication().endBackgroundTask(background_task)
    background_task = UIBackgroundTaskInvalid
}
coe720
  • 84
  • 2
  • possible duplicate of [How to Play Audio in Background Swift?](http://stackoverflow.com/questions/30280519/how-to-play-audio-in-background-swift) – Leo Dabus Jun 12 '15 at 18:13
  • I looked through that code and he doesn't appear to be using the Media Player framework at all besides importing it. I tried to stick the shared AVAudioSession code in my app but that didn't do anything. The app still ended the background task and my music stopped switching songs. – coe720 Jun 15 '15 at 09:07

1 Answers1

0

You don't need a background task for that.
Use the "Audio and AirPlay" background mode.
As long as you are playing audio, your app won't be suspended (unless iOS really needs it). enter image description here

Quentin Hayot
  • 7,786
  • 6
  • 45
  • 62
  • Well my app is definitely still playing audio, however, my logic that changes my music is longer running so the music just continues to play until the song is over and then it switches. I've tried running with both a systemMusicPlayer and an applicationMusicPlayer. – coe720 Jun 17 '15 at 20:14