Background audio not working with Xcode 7

2019-04-16 08:27发布

问题:

I have a radio player app, which streams audio from online radio stations. I have background modes enabled, like so:

When I built the app with Xcode 6, it worked on all devices and all iOS versions up to iOS 8. It even worked on an iPhone 6 that was later updated to iOS 9. Since then, I have updated to Xcode 7 and now when I build and run it on any device with any iOS version the background audio does not work, it just stops when the app goes to background.

Any ideas?

回答1:

In your plist make sure where it says "Application does not run in background" is NO.

Then add the following Code to your your appDelegate.m file in didFinishLaunchingWithOptions Method.

NSError *sessionError = nil;

NSError *activationError = nil;

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&sessionError];

[[AVAudioSession sharedInstance] setActive: YES error: &activationError];