ios - 在后台的预定点播放文本到语音

标签 ios swift xcode background text-to-speech

我需要在该应用程序关闭后在预定时间播放语音,并尝试使用以下代码来实现此目的:

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/

相关文章:

iphone - 快速在每个 View Controller 上滑动菜单

objective-c - UI 状态栏淡入淡出时间 iOS

iphone - 当目标对象已经为 NULL 时,iOS 如何捕获发送到实例的无法识别的选择器异常?

Swift - 无法获取 NSMutableArray() 的元素

c++ - <string.h> 与我自己的 String.h 冲突

xcode - App Store 图标(分层)未显示在我的 iTunes Connect 页面中

iphone - AVAudioPlayer 泄漏?

ios - 为什么我的相机(跟随玩家)不工作(SpriteKit)?

ios - 触摸文本框不起作用

ios - 使用 RxSwift 链接登录操作