如何在iOS 11中访问用户在设置中选择的Siri语音

8
我正在编写一个应用程序,其中包含使用AVSpeechSynthesizer的文本转语音功能。生成话语和使用语音合成器的代码一直很好地工作。
let utterance = AVSpeechUtterance(string: text)
utterance.voice = currentVoice
speechSynthesizer.speak(utterance)

现在使用iOS 11,我希望将语音与用户在手机设置应用中选择的语音匹配,但我没有找到获取该设置的任何方法。 我尝试获取已安装语音列表,并查找一个具有“quality”为“.enhanced”的语音,但有时没有安装增强语音,即使有,它也可能不是用户在设置应用程序中选择的语音。
static var enhanced: AVSpeechSynthesisVoice? {
    for voice in AVSpeechSynthesisVoice.speechVoices() {
        if voice.quality == .enhanced {
            return voice
        }
    }

    return nil
}

这些问题有两个方面:

  1. 如何确定用户在设置应用程序中选择了哪个语音?
  2. 为什么一些使用新的Siri语音的iOS 11手机上我找不到“增强”语音的安装?

enter image description here

3个回答

7

如果有一种方法可以选择与设置应用程序中相同的语音,那么它将在 AVSpeechSynthesisVoice 类的“查找语音”主题下显示。查看 AVSpeechSynthesisVoice 的代码定义时,我没有找到任何不同的检索语音的方法。

这是我正在处理的应用程序中获取增强语音的解决方法:

为了节省存储空间,新的 iOS 设备可能默认情况下没有增强版本的语音。在我的全新 iPhone 上迭代可用语音时,我只找到了默认质量语音,例如:[AVSpeechSynthesisVoice 0x1c4e11cf0] Language: en-US, Name: Samantha, Quality: Default [com.apple.ttsbundle.Samantha-compact]

我在这篇文章中找到了如何启用其他语音,并从中下载了名为“Samantha(增强版)”的语音。再次检查可用语音列表时,我注意到了以下添加内容:[AVSpeechSynthesisVoice 0x1c4c03060] Language: en-US, Name: Samantha (Enhanced), Quality: Enhanced [com.apple.ttsbundle.Samantha-premium]

目前,我已经能够在 Xcode 上选择增强语言。鉴于 AVSpeechSynthesisVoice.currentLanguageCode() 方法公开了当前选择的语言,我运行了以下代码来选择我能找到的第一个增强语音。如果没有可用的增强版,则只需选择默认版本(下面的代码是为我的应用程序中处理所有语音的 VoiceOver 自定义类创建的。下面的代码更新其语音变量)。

var voice: AVSpeechSynthesisVoice!

for availableVoice in AVSpeechSynthesisVoice.speechVoices(){
        if ((availableVoice.language == AVSpeechSynthesisVoice.currentLanguageCode()) &&
            (availableVoice.quality == AVSpeechSynthesisVoiceQuality.enhanced)){ // If you have found the enhanced version of the currently selected language voice amongst your available voices... Usually there's only one selected.
            self.voice = availableVoice
            print("\(availableVoice.name) selected as voice for uttering speeches. Quality: \(availableVoice.quality.rawValue)")
        }
}
if let selectedVoice = self.voice { // if sucessfully unwrapped, the previous routine was able to identify one of the enhanced voices
        print("The following voice identifier has been loaded: ",selectedVoice.identifier)
} else {
        self.voice = AVSpeechSynthesisVoice(language: AVSpeechSynthesisVoice.currentLanguageCode()) // load any of the voices that matches the current language selection for the device in case no enhanced voice has been found.

我也希望苹果能够公开一种直接加载所选语言的方法,但在此期间,我希望这个解决方法可以为您提供帮助。我猜Siri的增强语音是即时下载的,所以可能这就是它需要如此长时间来回答我的语音命令的原因 :)

最好的问候。


看起来您可以访问任何“命名”的语音,甚至使用其高级/增强版本,如果用户已将它们下载到设备上。Siri语音(我认为在macOS上是“Nora”,但不是在iOS上)似乎仍然无法访问。当我在设置中选择她作为我的默认语音并要求一个@"en-US"语音时,我得到了旧的、计算机化的Siri。遗憾。 - mbm29414
1
我编写了http://openradar.appspot.com/42656565来访问Siri的语音。请复制。 - steipete

2

看起来iOS 11中的新Siri语音不是AVSpeechSynthesis API的一部分,也不可用于开发人员。

在macOS 10.13 High Sierra(也有新的语音),似乎有一个新的SiriTTS框架与此功能相关,但它位于PrivateFrameworks中,因此没有开发人员API。


1
我会尽力提供更详细的答案。AVSpeechSynthesizer无法使用Siri语音。苹果已经锁定了此语音,以确保隐私,因为恶意应用程序可以冒充Siri并以此获取私人信息。

多年来,苹果一直没有改变这种情况,但目前正在进行相关的倡议。我们已经知道,使用权限可以访问iOS中的隐私敏感功能,并且没有理由不能在用户许可下访问Siri语音。您可以使用此请愿书投票支持此事,并希望苹果将来能够实现:https://www.change.org/p/apple-apple-please-allow-3rd-party-apps-to-use-siri-voices-for-improved-accessibility


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