0

当应用程序转换到后台时, AnAVAudioPlayer在后台任务中停止:

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
    }
}

AVAudioPlayer上面的函数由包含何时UIApplicationDidEnterBackground发布通知的视图控制器调用。

我仍然在行出现错误backgroundTask = UIBackgroundTaskInvalid

// Can't endBackgroundTask: no background task exists with identifier 1f, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.`

当我删除该行时,我没有收到任何错误。我查看了关于 SO 的类似问题,但它们没有帮助。

是什么导致了这个错误?

4

0 回答 0