AVSpeechSynthesizer is marked as available on macOS Mojave beta.
以前它仅适用于 iOS、tvOS 和 watchOS。但是如果我在 Xcode 10 中准备一个小的 macOS 测试项目,它会给我一个错误“使用未解析的标识符‘AVSpeechSynthesizer’”。在顶部,我有:
import Cocoa
import NaturalLanguage
import AVFoundation
我的代码是:
let string = "Mickey mouse went to town"
let recognizer = NLLanguageRecognizer()
recognizer.processString(string)
let language = recognizer.dominantLanguage!.rawValue
let speechSynthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: string)
utterance.voice = AVSpeechSynthesisVoice(language: language)
speechSynthesizer.speak(utterance)
它与 iOS 上的代码完全相同,但在 iOS 上它可以工作,在 macOS 上它会给出错误。任何帮助深表感谢。谢谢
最佳答案
目前 Mojave 上的 AVFAudio.h 有这个:
#if TARGET_OS_IPHONE
#import <AVFAudio/AVAudioSession.h>
#import <AVFAudio/AVSpeechSynthesis.h>
#endif
AVSpeechSynthesizer
和 friend 在 AVSpeechSynthesis.h 中声明。这是您看到该错误的技术原因,这些 header 仅在为 iOS 编译时包含。
但我已尝试手动导入该 header ,但我认为 AVSpeechSynthesizer
在 Mojave 上无法正常工作,即使 #if 不存在也是如此。尝试在 Mojave 上创建 AVSpeechUtterance
总是返回 nil。
所以这要么是 Mojave 上未完成的工作,要么标题注释不正确,并且无法通过 Mojave 上的 AVFoundation 进行语音合成。请注意,NSSpeechSynthesizer
仍然存在,并且没有被标记为弃用。
关于swift - 在 macOS Mojave 上,在 cocoa 应用程序中,如何使用 AVSpeechSynthesizer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51732571/