How to access Siri voice selected by user in Settings in iOS 11

pic*_*ano 5 avfoundation ios siri swift

I am writing an app that includes text-to-speech using AVSpeechSynthesizer. The code for generating the utterance and using the speech synthesizer has been working fine.

let utterance = AVSpeechUtterance(string: text)
utterance.voice = currentVoice
speechSynthesizer.speak(utterance)
Run Code Online (Sandbox Code Playgroud)

Now with iOS 11, I want to match the voice to the one selected by the user in the phone's Settings app, but I do not see any way to get that setting.

我试图让安装声音列表,并寻找一个拥有quality.enhanced,但有时有没有增强的语音安装,即使有,那可能是也可能不是在设置应用程序的用户选择的声音。

static var enhanced: AVSpeechSynthesisVoice? {
    for voice in AVSpeechSynthesisVoice.speechVoices() {
        if voice.quality == .enhanced {
            return voice
        }
    }

    return nil
}
Run Code Online (Sandbox Code Playgroud)

问题是双重的:

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

在此处输入图片说明

And*_*rra 6

我想如果有一种方法可用于选择与“设置”应用程序中相同的声音,它将显示在“查找声音”主题下的 AVSpeechSynthesisVoice 类的文档中。跳转到 AVSpeechSynthesisVoice 代码中的定义,我找不到任何不同的方法来检索语音。

这是我为我正在开发的应用程序增强语音的解决方法:

为了节省存储空间,默认情况下,新的 iOS 设备中可能不存在增强版本的语音。在我全新的 iPhone 上遍历可用的声音,我只找到了默认质量的声音,例如:[AVSpeechSynthesisVoice 0x1c4e11cf0] 语言:en-US,名称:Samantha,质量:默认 [com.apple.ttsbundle.Samantha-compact]

我找到了这篇关于如何启用附加语音的文章,并在其中下载了名为“Samantha (Enhanced)”的文章。再次检查可用语音列表,我注意到以下添加: [AVSpeechSynthesisVoice 0x1c4c03060] 语言:en-US,名称:Samantha(增强),质量:增强 [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.
Run Code Online (Sandbox Code Playgroud)

}

我也希望 Apple 能够公开一种直接加载所选语言的方法,但同时我希望这项工作可以为您服务。我想 Siri 的增强语音是在旅途中下载的,所以也许这就是我的语音命令需要这么长时间才能回答的原因:)

此致。