背景
支持的语音
截至2020年5月7日,print(AVSpeechSynthesisVoice.speechVoices())
报告了以下支持的语音(iOS 13.3.1;iPhone而不是模拟器):
[AVSpeechSynthesisVoice 0x28266f9c0] Language: ar-SA, Name: Maged, Quality: Default [com.apple.ttsbundle.Maged-compact]
[AVSpeechSynthesisVoice 0x28266dd10] Language: cs-CZ, Name: Zuzana, Quality: Default [com.apple.ttsbundle.Zuzana-compact]
[AVSpeechSynthesisVoice 0x28266f050] Language: da-DK, Name: Sara, Quality: Default [com.apple.ttsbundle.Sara-compact]
[AVSpeechSynthesisVoice 0x28266ef90] Language: de-DE, Name: Anna, Quality: Default [com.apple.ttsbundle.Anna-compact]
[AVSpeechSynthesisVoice 0x28266f710] Language: de-DE, Name: Helena, Quality: Default [com.apple.ttsbundle.siri_female_de-DE_compact]
[AVSpeechSynthesisVoice 0x28266d8f0] Language: de-DE, Name: Martin, Quality: Default [com.apple.ttsbundle.siri_male_de-DE_compact]
[AVSpeechSynthesisVoice 0x28266fbf0] Language: el-GR, Name: Melina, Quality: Default [com.apple.ttsbundle.Melina-compact]
[AVSpeechSynthesisVoice 0x28266f9b0] Language: en-AU, Name: Catherine, Quality: Default [com.apple.ttsbundle.siri_female_en-AU_compact]
[AVSpeechSynthesisVoice 0x28266ea00] Language: en-AU, Name: Gordon, Quality: Default [com.apple.ttsbundle.siri_male_en-AU_compact]
[AVSpeechSynthesisVoice 0x28266f5e0] Language: en-AU, Name: Karen, Quality: Default [com.apple.ttsbundle.Karen-compact]
[AVSpeechSynthesisVoice 0x28266e9a0] Language: en-GB, Name: Arthur, Quality: Default [com.apple.ttsbundle.siri_male_en-GB_compact]
[AVSpeechSynthesisVoice 0x28266fba0] Language: en-GB, Name: Daniel, Quality: Default [com.apple.ttsbundle.Daniel-compact]
[AVSpeechSynthesisVoice 0x28266f880] Language: en-GB, Name: Martha, Quality: Default [com.apple.ttsbundle.siri_female_en-GB_compact]
[AVSpeechSynthesisVoice 0x28266d0e0] Language: en-IE, Name: Moira, Quality: Default [com.apple.ttsbundle.Moira-compact]
[AVSpeechSynthesisVoice 0x28266efe0] Language: en-IN, Name: Rishi, Quality: Default [com.apple.ttsbundle.Rishi-compact]
[AVSpeechSynthesisVoice 0x2826327e0] Language: en-US, Name: Aaron, Quality: Default [com.apple.ttsbundle.siri_male_en-US_compact]
[AVSpeechSynthesisVoice 0x28266fb40] Language: en-US, Name: Fred, Quality: Default [com.apple.speech.synthesis.voice.Fred]
[AVSpeechSynthesisVoice 0x28266ce30] Language: en-US, Name: Nicky, Quality: Default [com.apple.ttsbundle.siri_female_en-US_compact]
[AVSpeechSynthesisVoice 0x28266ed60] Language: en-US, Name: Samantha, Quality: Default [com.apple.ttsbundle.Samantha-compact]
[AVSpeechSynthesisVoice 0x28266f170] Language: en-ZA, Name: Tessa, Quality: Default [com.apple.ttsbundle.Tessa-compact]
[AVSpeechSynthesisVoice 0x28266ef60] Language: es-ES, Name: Mónica, Quality: Default [com.apple.ttsbundle.Monica-compact]
[AVSpeechSynthesisVoice 0x28266ed30] Language: es-MX, Name: Paulina, Quality: Default [com.apple.ttsbundle.Paulina-compact]
[AVSpeechSynthesisVoice 0x28266d2d0] Language: fi-FI, Name: Satu, Quality: Default [com.apple.ttsbundle.Satu-compact]
[AVSpeechSynthesisVoice 0x28266fe40] Language: fr-CA, Name: Amélie, Quality: Default [com.apple.ttsbundle.Amelie-compact]
[AVSpeechSynthesisVoice 0x28266f620] Language: fr-FR, Name: Daniel, Quality: Default [com.apple.ttsbundle.siri_male_fr-FR_compact]
[AVSpeechSynthesisVoice 0x28266fbc0] Language: fr-FR, Name: Marie, Quality: Default [com.apple.ttsbundle.siri_female_fr-FR_compact]
[AVSpeechSynthesisVoice 0x28266fb20] Language: fr-FR, Name: Thomas, Quality: Default [com.apple.ttsbundle.Thomas-compact]
[AVSpeechSynthesisVoice 0x28266fd30] Language: he-IL, Name: Carmit, Quality: Default [com.apple.ttsbundle.Carmit-compact]
[AVSpeechSynthesisVoice 0x28266d820] Language: hi-IN, Name: Lekha, Quality: Default [com.apple.ttsbundle.Lekha-compact]
[AVSpeechSynthesisVoice 0x28266f070] Language: hu-HU, Name: Mariska, Quality: Default [com.apple.ttsbundle.Mariska-compact]
[AVSpeechSynthesisVoice 0x28266ed70] Language: id-ID, Name: Damayanti, Quality: Default [com.apple.ttsbundle.Damayanti-compact]
[AVSpeechSynthesisVoice 0x282631210] Language: it-IT, Name: Alice, Quality: Default [com.apple.ttsbundle.Alice-compact]
[AVSpeechSynthesisVoice 0x28266ef30] Language: ja-JP, Name: Hattori, Quality: Default [com.apple.ttsbundle.siri_male_ja-JP_compact]
[AVSpeechSynthesisVoice 0x28266d510] Language: ja-JP, Name: Kyoko, Quality: Default [com.apple.ttsbundle.Kyoko-compact]
[AVSpeechSynthesisVoice 0x28266e840] Language: ja-JP, Name: O-ren, Quality: Default [com.apple.ttsbundle.siri_female_ja-JP_compact]
[AVSpeechSynthesisVoice 0x28266ea90] Language: ko-KR, Name: Yuna, Quality: Default [com.apple.ttsbundle.Yuna-compact]
[AVSpeechSynthesisVoice 0x28266f6e0] Language: nl-BE, Name: Ellen, Quality: Default [com.apple.ttsbundle.Ellen-compact]
[AVSpeechSynthesisVoice 0x28266f6a0] Language: nl-NL, Name: Xander, Quality: Default [com.apple.ttsbundle.Xander-compact]
[AVSpeechSynthesisVoice 0x28266dc80] Language: no-NO, Name: Nora, Quality: Default [com.apple.ttsbundle.Nora-compact]
[AVSpeechSynthesisVoice 0x28266d020] Language: pl-PL, Name: Zosia, Quality: Default [com.apple.ttsbundle.Zosia-compact]
[AVSpeechSynthesisVoice 0x28266e930] Language: pt-BR, Name: Luciana, Quality: Default [com.apple.ttsbundle.Luciana-compact]
[AVSpeechSynthesisVoice 0x28266e1b0] Language: pt-PT, Name: Joana, Quality: Default [com.apple.ttsbundle.Joana-compact]
[AVSpeechSynthesisVoice 0x28266cfb0] Language: ro-RO, Name: Ioana, Quality: Default [com.apple.ttsbundle.Ioana-compact]
[AVSpeechSynthesisVoice 0x28266ee50] Language: ru-RU, Name: Milena, Quality: Default [com.apple.ttsbundle.Milena-compact]
[AVSpeechSynthesisVoice 0x28266eee0] Language: sk-SK, Name: Laura, Quality: Default [com.apple.ttsbundle.Laura-compact]
[AVSpeechSynthesisVoice 0x28266e070] Language: sv-SE, Name: Alva, Quality: Default [com.apple.ttsbundle.Alva-compact]
[AVSpeechSynthesisVoice 0x28266d470] Language: th-TH, Name: Kanya, Quality: Default [com.apple.ttsbundle.Kanya-compact]
[AVSpeechSynthesisVoice 0x28266f1d0] Language: tr-TR, Name: Yelda, Quality: Default [com.apple.ttsbundle.Yelda-compact]
[AVSpeechSynthesisVoice 0x28266db10] Language: zh-CN, Name: Tian-Tian (Enhanced), Quality: Enhanced [com.apple.ttsbundle.Ting-Ting-premium]
[AVSpeechSynthesisVoice 0x28266d890] Language: zh-CN, Name: Li-mu, Quality: Default [com.apple.ttsbundle.siri_male_zh-CN_compact]
[AVSpeechSynthesisVoice 0x28266d4c0] Language: zh-CN, Name: Tian-Tian, Quality: Default [com.apple.ttsbundle.Ting-Ting-compact]
[AVSpeechSynthesisVoice 0x28266ea60] Language: zh-CN, Name: Yu-shu, Quality: Default [com.apple.ttsbundle.siri_female_zh-CN_compact]
[AVSpeechSynthesisVoice 0x28266f140] Language: zh-HK, Name: Sin-Ji, Quality: Default [com.apple.ttsbundle.Sin-Ji-compact]
[AVSpeechSynthesisVoice 0x28266ef10] Language: zh-TW, Name: Mei-Jia, Quality: Default [com.apple.ttsbundle.Mei-Jia-compact]
[AVAlexSpeechSynthesisVoice 0x282630950] Language: en-US, Name: Alex, Quality: Enhanced [com.apple.speech.voice.Alex]
如下评论所述,这显然不包括在iPhone偏好设置中可用的
en-AU
男声“Lee”。看起来他目前不可通过此API使用。也许他将来会被添加进来,但考虑到可用的语音似乎自
2015年12月30日以来并没有改变,我不会抱太大希望。
设置
import AVFoundation
let utterance : AVSpeechUtterance = AVSpeechUtterance(string: "String to be read out.")
let voiceToUse: AVSpeechSynthesisVoice? = AVSpeechSynthesisVoice(language: "en-US")
utterance.voice = voiceToUse!
AVSpeechSynthesizer().speak(utterance)
答案
我们可以直接选择Alex的声音,使用AVSpeechSynthesisVoice(identifier: AVSpeechSynthesisVoiceIdentifierAlex)
,但奇怪的是,到目前为止Apple提供的唯一标识符是Alex。
对于任何其他语音(请参阅上面的语音名称列表),请使用以下代码:
var voiceToUse: AVSpeechSynthesisVoice?
for voice in AVSpeechSynthesisVoice.speechVoices() {
if #available(iOS 9.0, *) {
if voice.name == "Karen" {
voiceToUse = voice
}
}
}
(使用filter()
比循环更优雅地完成此操作,但我不熟悉Swift中的语法,因此如果您感兴趣,请参考这里,或者如果您知道如何编写它,请留下评论)。
至于标记为“Siri”的声音,我不知道。也许它们与AVSpeechSynthesisVoice
无关?如果您知道这些背后的原因,请留下评论。
speechVoices
和 iPhone 偏好设置中都只有这一个。就我所听到的(普通话语音非常明显),只要已经下载了增强语音,就会自动使用增强语音而不是普通语音。 - Jamie Birch