An AVAudioPlayer
is stopped in a background tasks when the app transitions to background:
func stop() {
var backgroundTask: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid
backgroundTask = UIApplication.shared.beginBackgroundTask() {
UIApplication.shared.endBackgroundTask(backgroundTask)
backgroundTask = UIBackgroundTaskInvalid
}
DispatchQueue.global(qos: .default).async {
self.audioPlayer?.pause()
UIApplication.shared.endBackgroundTask(backgroundTask)
backgroundTask = UIBackgroundTaskInvalid // Symbolic breakpoint stops here
}
}
The function above gets called by the view controller that contains the AVAudioPlayer
when a UIApplicationDidEnterBackground
notification is posted.
Still I get an error at line backgroundTask = UIBackgroundTaskInvalid
:
// Can't endBackgroundTask: no background task exists with identifier 1f, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.`
When I remove the line I get no errors. I looked through similar questions on SO but they did not help.
What is causing this error?