代码之家  ›  专栏  ›  技术社区  ›  Alfonso Tesauro

在Macos Mojave上,在Cocoa应用程序中,如何使用AvSpeechSynther?

  •  2
  • Alfonso Tesauro  · 技术社区  · 6 年前

    AVSpeechSynthesizer is marked as available on macOS Mojave beta.

    以前它只在iOS、tvos和watchos上提供。但是,如果我在Xcode10中准备一个小型MacOS测试项目,它会给我一个错误“使用未解析的标识符‘avspeech合成器’”。在顶部,我有:

    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上它给出了错误。任何帮助都非常感谢。谢谢

    1 回复  |  直到 6 年前
        1
  •  1
  •   pfandrade    6 年前

    目前莫哈韦的avfaudio.h有:

    #if TARGET_OS_IPHONE
    #import <AVFAudio/AVAudioSession.h>
    #import <AVFAudio/AVSpeechSynthesis.h>
    #endif
    

    AVSpeechSynthesizer 而friends则在avspeechsynthesis.h中声明。这是您看到错误的技术原因,这些头只在为iOS编译时包含。

    但我试过手动导入那个头,我不认为 语音合成器 在莫哈韦工作是否正常,即使没有。尝试创建一个 AVSpeechUtterance 莫哈韦队的总比分是零。

    因此,这要么是未完成的mojave工作,要么是标题注释错误,无法通过mojave上的avfoundation进行语音合成。注意 NSSpeechSynthesizer 仍然存在且未标记为已弃用。