I'm using AudioKit to play sounds in my app and I'd like to have the audio keep playing in the background. But when the app goes into the background or when I go into the lock screen on my iPhone the audio from the app stops.
I've enabled the background audio capability in XCode (and confirmed that the appropriate item was added to Info.plist).
I've set the session category to .playback which, according to the comments in AKSettings.swift:
Audio is not silenced by silent switch and screen lock - audio is non mixable
So my understanding is that if I set this category and enable background audio in my Info.plist then the audio should be audible even in the lock screen, but instead the audio goes silent (until I unlock the phone and go back to the app).
I've cooked up a very minimal example which requires no files other than the app delegate (and of course, AudioKit):
import UIKit
import AudioKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = UIViewController()
self.window?.makeKeyAndVisible()
// The important bits:
try? AKSettings.setSession(category: .playback)
AudioKit.output = AKWhiteNoise()
try? AudioKit.start()
return true
}
}
As you can see, all I'm doing is setting the session category and playing some white noise. When I launch the app I can hear the white noise just fine, it only goes silent when the phone is locked or the app is in the background.
Another thing worth noting: When I replace:
try? AKSettings.setSession(category: .playback)
with:
try? AKSettings.setSession(category: .playAndRecord)
let s = AKStereoInput()
Then the audio does keep playing in the background/locked screen, but then the flashing red recording system bar appears at the top. So presumably the app is kept in the background because AKStereoInput assigns an input node when it gets initialized.
Other things worth noting:
- I'm testing on a device (iPhone 6), with no headphones or other hardware plugged in.
- No other apps are running during this test.
Am I missing something obvious? Maybe a setting or something? Or should I file an issue on the AudioKit github?