获取“所有”AVSpeechSynthesisVoice对象列表的标准方法是询问它:
[AVSpeechSynthesisVoice speechVoices]
但是,这仅显示每个语言区域对的唯一默认语音。例如对于en-US:
"Language: en-US, Name: Samantha, Quality: Default",
如果我查看下面的“设置”应用 常规→辅助功能→语音→语音→英语 我看到我选择了'Siri Female(增强版)',但是这个声音无法从代码中选择。 完整列表是
所以最大的问题是:我如何选择例如亚历克斯或Siri女性作为代码的声音?
答案 0 :(得分:9)
AVSpeechSynthesisVoice
documentation页。
语音的语言应以BCP-47格式指定。请参阅下面的可用语音(以及相应的BCP-47语言代码的含义)。
截至2017年4月28日,print(AVSpeechSynthesisVoice.speechVoices())
报告以下支持的声音:
[AVSpeechSynthesisVoice 0x6080000097d0] Language: ar-SA, Name: Maged, Quality: Default [com.apple.ttsbundle.Maged-compact]
[AVSpeechSynthesisVoice 0x608000009780] Language: cs-CZ, Name: Zuzana, Quality: Default [com.apple.ttsbundle.Zuzana-compact]
[AVSpeechSynthesisVoice 0x608000009440] Language: da-DK, Name: Sara, Quality: Default [com.apple.ttsbundle.Sara-compact]
[AVSpeechSynthesisVoice 0x608000009670] Language: de-DE, Name: Anna, Quality: Default [com.apple.ttsbundle.Anna-compact]
[AVSpeechSynthesisVoice 0x6080000092e0] Language: el-GR, Name: Melina, Quality: Default [com.apple.ttsbundle.Melina-compact]
[AVSpeechSynthesisVoice 0x608000009310] Language: en-AU, Name: Karen, Quality: Default [com.apple.ttsbundle.Karen-compact]
[AVSpeechSynthesisVoice 0x6080000098d0] Language: en-GB, Name: Daniel, Quality: Default [com.apple.ttsbundle.Daniel-compact]
[AVSpeechSynthesisVoice 0x6080000094e0] Language: en-IE, Name: Moira, Quality: Default [com.apple.ttsbundle.Moira-compact]
[AVSpeechSynthesisVoice 0x6080000096b0] Language: en-US, Name: Samantha, Quality: Default [com.apple.ttsbundle.Samantha-compact]
[AVSpeechSynthesisVoice 0x608000009480] Language: en-ZA, Name: Tessa, Quality: Default [com.apple.ttsbundle.Tessa-compact]
[AVSpeechSynthesisVoice 0x6080000093d0] Language: es-ES, Name: Monica, Quality: Default [com.apple.ttsbundle.Monica-compact]
[AVSpeechSynthesisVoice 0x6080000096c0] Language: es-MX, Name: Paulina, Quality: Default [com.apple.ttsbundle.Paulina-compact]
[AVSpeechSynthesisVoice 0x608000009550] Language: fi-FI, Name: Satu, Quality: Default [com.apple.ttsbundle.Satu-compact]
[AVSpeechSynthesisVoice 0x6080000094f0] Language: fr-CA, Name: Amelie, Quality: Default [com.apple.ttsbundle.Amelie-compact]
[AVSpeechSynthesisVoice 0x608000009880] Language: fr-FR, Name: Thomas, Quality: Default [com.apple.ttsbundle.Thomas-compact]
[AVSpeechSynthesisVoice 0x608000009290] Language: he-IL, Name: Carmit, Quality: Default [com.apple.ttsbundle.Carmit-compact]
[AVSpeechSynthesisVoice 0x608000009570] Language: hi-IN, Name: Lekha, Quality: Default [com.apple.ttsbundle.Lekha-compact]
[AVSpeechSynthesisVoice 0x608000009510] Language: hu-HU, Name: Mariska, Quality: Default [com.apple.ttsbundle.Mariska-compact]
[AVSpeechSynthesisVoice 0x608000009040] Language: id-ID, Name: Damayanti, Quality: Default [com.apple.ttsbundle.Damayanti-compact]
[AVSpeechSynthesisVoice 0x6080000096a0] Language: it-IT, Name: Alice, Quality: Default [com.apple.ttsbundle.Alice-compact]
[AVSpeechSynthesisVoice 0x6080000092b0] Language: ja-JP, Name: Kyoko, Quality: Default [com.apple.ttsbundle.Kyoko-compact]
[AVSpeechSynthesisVoice 0x608000009540] Language: ko-KR, Name: Yuna, Quality: Default [com.apple.ttsbundle.Yuna-compact]
[AVSpeechSynthesisVoice 0x6080000090e0] Language: nl-BE, Name: Ellen, Quality: Default [com.apple.ttsbundle.Ellen-compact]
[AVSpeechSynthesisVoice 0x608000009380] Language: nl-NL, Name: Xander, Quality: Default [com.apple.ttsbundle.Xander-compact]
[AVSpeechSynthesisVoice 0x608000009240] Language: no-NO, Name: Nora, Quality: Default [com.apple.ttsbundle.Nora-compact]
[AVSpeechSynthesisVoice 0x6080000090f0] Language: pl-PL, Name: Zosia, Quality: Default [com.apple.ttsbundle.Zosia-compact]
[AVSpeechSynthesisVoice 0x608000009250] Language: pt-BR, Name: Luciana, Quality: Default [com.apple.ttsbundle.Luciana-compact]
[AVSpeechSynthesisVoice 0x6080000094d0] Language: pt-PT, Name: Joana, Quality: Default [com.apple.ttsbundle.Joana-compact]
[AVSpeechSynthesisVoice 0x608000009210] Language: ro-RO, Name: Ioana, Quality: Default [com.apple.ttsbundle.Ioana-compact]
[AVSpeechSynthesisVoice 0x608000009130] Language: ru-RU, Name: Milena, Quality: Default [com.apple.ttsbundle.Milena-compact]
[AVSpeechSynthesisVoice 0x608000009620] Language: sk-SK, Name: Laura, Quality: Default [com.apple.ttsbundle.Laura-compact]
[AVSpeechSynthesisVoice 0x608000009730] Language: sv-SE, Name: Alva, Quality: Default [com.apple.ttsbundle.Alva-compact]
[AVSpeechSynthesisVoice 0x608000009740] Language: th-TH, Name: Kanya, Quality: Default [com.apple.ttsbundle.Kanya-compact]
[AVSpeechSynthesisVoice 0x6080000095e0] Language: tr-TR, Name: Yelda, Quality: Default [com.apple.ttsbundle.Yelda-compact]
[AVSpeechSynthesisVoice 0x608000009010] Language: zh-CN, Name: Ting-Ting, Quality: Default [com.apple.ttsbundle.Ting-Ting-compact]
[AVSpeechSynthesisVoice 0x608000009660] Language: zh-HK, Name: Sin-Ji, Quality: Default [com.apple.ttsbundle.Sin-Ji-compact]
[AVSpeechSynthesisVoice 0x608000009860] Language: zh-TW, Name: Mei-Jia, Quality: Default [com.apple.ttsbundle.Mei-Jia-compact]
正如Axello在下面评论的那样,这显然不包括iPhone首选项中提供的en-AU
男声," Lee"。看起来他目前无法通过此API获得。也许他会及时补充,但鉴于可用的声音看起来不像他们已经改变since 30th December 2015,我不会为此屏住呼吸。
import AVFoundation
let utterance : AVSpeechUtterance = AVSpeechUtterance(string: "String to be read out.")
let voiceToUse: AVSpeechSynthesisVoice? = AVSpeechSynthesisVoice(language: "en-US")
utterance.voice = voiceToUse! // Set voice by BCP-47 language code.
AVSpeechSynthesizer().speak(utterance) // Note: Won't be heard in Swift Playground.
我们可以直接选择Alex的语音,使用AVSpeechSynthesisVoice(identifier: AVSpeechSynthesisVoiceIdentifierAlex)
,但奇怪的是,Apple迄今为止提供的唯一标识符是Alex。
对于任何其他声音(请再次参阅上面的语音名称列表),请使用以下代码:
// From https://www.ikiapps.com/tips/2015/12/30/setting-voice-for-tts-in-ios
var voiceToUse: AVSpeechSynthesisVoice?
for voice in AVSpeechSynthesisVoice.speechVoices() {
if #available(iOS 9.0, *) {
if voice.name == "Karen" {
voiceToUse = voice
}
}
}
(这可以使用filter()
比使用循环更优雅地完成,但我不熟悉Swift中的语法,所以如果感兴趣请参考here或留下评论,如果你知道如何写它。)
至于标有“Siri'”的声音,我不知道。也许他们与AVSpeechSynthesisVoice
无关?如果你知道他们背后的交易,欢迎评论。
答案 1 :(得分:3)
跟进Jamie的回答(现在我认为iOS 11.3也适用于早期版本):
对于其他声音,标识符的结构为" com.apple.ttsbundle.Kanya-compact"
您可以通过以下方式获取可用的内容:
let voices = AVSpeechSynthesisVoice.speechVoices()
for voice in voices {
print (voice.identifier + " " + s.language + " " + s.name)
}
并通过标识符或语言设置语音。
//set utterance first!
speechUtter = AVSpeechUtterance(string:"أتمنى لك نهارا سعيد")
speechUtter.voice = AVSpeechSynthesisVoice(identifier: "com.apple.ttsbundle.Maged-compact")
或
speechUtter.voice = AVSpeechSynthesisVoice(language: "ar-SA")
答案 2 :(得分:0)
您可以使用此简单命令
print(AVSpeechSynthesisVoice.speechVoices())