我正在编写一个应用程序,其中包括文字到语音使用AVSpeechSynthesizer。生成语音和使用语音合成器的代码一直运行良好。
let utterance = AVSpeechUtterance(string: text)
utterance.voice = currentVoice
speechSynthesizer.speak(utterance)现在使用iOS 11,我想匹配用户在手机的设置应用程序中选择的声音,但我没有看到任何方法来获得该设置。
我试着获取已安装的声音列表,并寻找具有quality of .enhanced的声音,但有时没有安装增强的语音,即使有,也可能是用户在设置应用程序中选择的声音。
static var enhanced: AVSpeechSynthesisVoice? {
for voice in AVSpeechSynthesisVoice.speechVoices() {
if voice.quality == .enhanced {
return voice
}
}
return nil
}这些问题有两个:

发布于 2017-10-17 18:50:42
我想,如果有一种方法可以选择与Settings应用程序中相同的声音,那么它将显示在AVSpeechSynthesisVoice类的文档中,在“查找声音”主题下。跳到AVSpeechSynthesisVoice代码中的定义,我找不到任何不同的方法来检索声音。
以下是我在为我正在开发的应用程序获得一个增强的声音方面的解决办法:
为了节省存储,默认情况下,新的iOS设备中可能不存在增强版本的声音。在我的全新的com.apple.ttsbundle.Samantha-compact上迭代可用的声音,我只找到默认的质量声音,例如:AVSpeechSynthesisVoice 0x1c4e11cf0语言: en-US,Name: Samantha,iPhone : Default
我找到了这个关于如何在声音上启用额外声音的文章,并下载了其中一个名为“Samantha(增强的)”的声音。再次检查可用的声音列表时,我注意到了以下添加:AVSpeechSynthesisVoice 0x1c4c03060语言: en-US,Name: Samantha (增强),AVSpeechSynthesisVoice 0x1c4c03060
到目前为止,我能够在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的增强语音是在路上下载的,所以也许这就是为什么回答我的语音命令需要这么长时间的原因:)
诚挚的问候。
发布于 2017-10-17 20:27:12
看起来,iOS 11中的新Siri语音不是AVSpeechSynthesis API的一部分,开发者也无法使用。
在macOS 10.13 High塞拉利昂(它也获得了新的声音)中,似乎有一个新的SiriTTS框架可能与此功能相关,但它在PrivateFrameworks中,所以它没有一个开发者API。
发布于 2021-10-05 11:30:36
我会尽量提供一个更详细的答案。AVSpeechSynthesizer不能使用Siri语音。苹果已经锁定了这个声音,以确保隐私,因为恶意应用程序可以模仿Siri,并以这种方式获取私密信息。
苹果公司多年来一直没有改变这一状况,但在这方面仍在采取主动行动。我们已经知道有一种解决方案可以使用权限访问iOS中的隐私敏感特性,没有理由不能使用用户权限访问Siri语音。你可以使用这份请愿书投票支持这件事,并希望苹果将来能实现这一点:https://www.change.org/p/apple-apple-please-allow-3rd-party-apps-to-use-siri-voices-for-improved-accessibility
https://stackoverflow.com/questions/46332732
复制相似问题