我需要在该应用程序关闭后在预定时间播放语音,并尝试使用以下代码来实现此目的:
DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
print("here")
let utterance = AVSpeechUtterance(string: "Test speech")
utterance.rate = AVSpeechUtteranceDefaultSpeechRate
self.synthesizer.speak(utterance)
}
在应用程序委托(delegate)的 applicationDidEnterBackground() 函数中,但它仅适用于较小的秒数,例如 2,而不是 10。
应用程序进入后台后如何播放语音?
谢谢。
最佳答案
您不能执行此操作(至少在未越狱的设备上不能合法执行)。您的应用程序在后台事件 5 秒,然后暂停(少数情况除外,恕我直言,播放文本转语音不算在内):
When your app moves to the background, the system calls your app delegate’s applicationDidEnterBackground(_:) method. That method has five seconds to perform any tasks and return. Shortly after that method returns, the system puts your app into the suspended state.
(来自 Apple Docs "Extending Your App's Background Execution Time" )
关于ios - 在后台的预定点播放文本到语音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52033514/