在macOS上获取Siri语音的NSSpeechSynthesizer

10
有没有办法获取NSSpeechSynthesizer的Siri语音?NSSpeechSynthesizer.availableVoices()没有列出它们,但也许有一些未记录的技巧或者其他方法吗?
我还试过使用AVSpeechSynthesizer,即使它应该在macOS 10.14+上可用,我也无法将其朗读出来...
我使用Playground测试了以下代码:NSHipster提供的:
import Cocoa
import AVFoundation

let string = "Hello, World!"
let utterance = AVSpeechUtterance(string: string)

let synthesizer = AVSpeechSynthesizer()
synthesizer.speak(utterance)

“我无法让它大声朗读”,您能具体说明您尝试了什么吗? - vrwim
@vrwim,我基本上使用了_zrzka_在他的答案中建议的代码(我也更新了我的代码)。虽然我仍然无法让我的Mac实际朗读出字符串。尽管如此,我的主要目标是获得(更好的)Siri语音。这似乎是不可能使用公共API…… - alexkaessner
1个回答

6

Siri语音

我不知道有什么技巧可以让Siri语音可用,恐怕无法通过公共API使其工作。

  • NSSpeechSynthesizerAVSpeechSynthesizer都使用SpeechSynthesis.framework(包含在ApplicationServices.framework中)。
  • 快速检查了一下,函数BuildDefaultVoiceList(bool)会检查捆绑标识符(通过PermitAllVoices,将其与com.apple.VoiceOverUtilitycom.apple.VoiceOvercom.apple.VoiceOverUtilityCacheBuilder进行比较)。
  • 可能还有其他检查。

所有的语音都存储在/System/Library/Speech/Voices文件夹中。尝试复制语音,更改Info.plist值,使其看起来像另一个语音,但仍然无法使用/工作。

你想走多远?你想禁用SIP,修改框架,预加载你的东西,...这值得吗?

macOS上的AVSpeech​Synthesizer

它能够工作,但是存在缺陷,你需要时不时地杀掉服务。

可用语音列表

AVSpeechSynthesisVoice.speechVoices().forEach { voice in
    print("Name: \(voice.name) Language: \(voice.language) Identifier: \(voice.identifier)")
}

讲解

let utterance = AVSpeechUtterance(string: "Ahoj, jak se máš?")
utterance.voice = AVSpeechSynthesisVoice(identifier: "com.apple.speech.synthesis.voice.zuzana.premium")

let synthesizer = AVSpeechSynthesizer()
synthesizer.speak(utterance)

无法说话?

当您调用speak(utterance)时,所有以下方法(委托)都会被调用,并且它的工作正常:

如果您只收到speechSynthesizer(_:didCancel:)方法调用,并且必须要杀死speechsynthesisd进程,则存在问题:

kill `pgrep speechsynthesisd`

那么请再试一次,对我来说这修复了问题。

安装额外语音

  • 系统偏好设置 - 辅助功能 - 语音
  • 系统语音 - 点击并向下滚动 - 选择定制...
  • 选择要安装的额外语音

非常感谢@zrzka提供这个深入的答案!我的主要目标是获取Siri的声音,但是正如你确认的那样,似乎不可能。我希望能够通过较新的AVSpeechSynthesizer API访问它们。不过,如果它指向与NSSpeechSynthesizer相同的框架,那么我现在会坚持使用它。 - alexkaessner

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接