How to get a list of ALL voices on iOS 9?

限于喜欢 提交于 2020-06-24 08:45:53

问题


The standard way to get a list of 'all' AVSpeechSynthesisVoice objects, is to ask for it:

[AVSpeechSynthesisVoice speechVoices]

However, this only shows a unique default voice per language-region pair. e.g. for en-US:

"Language: en-US, Name: Samantha, Quality: Default",

If I look in the Settings app under General → Accessibility → Speech → Voices → English I see that I have 'Siri Female (Enhanced)' selected, but that voice is not selectable from code. The full list is

1

So the big question is: how do I select e.g. Alex or Siri Female as a voice from code?


回答1:


Background

  • AVSpeechSynthesisVoice documentation page.

  • Languages for the voices should be specified in BCP-47 format. See below for the available voices, (and what their corresponding BCP-47 language codes mean).

Supported voices

As of 7th May 2020, print(AVSpeechSynthesisVoice.speechVoices()) reports the following supported voices (iOS 13.3.1; iPhone rather than simulator):

[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]

As Axello comments below, this evidently does not include the en-AU male voice that is available in the iPhone preferences, "Lee". It would seem that he is not currently available through this API. Perhaps he will be added in time, but given that the available voices don't look like they've changed since 30th December 2015, I wouldn't hold my breath for it.

Setup

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.

Answer

We can select Alex's voice directly, using AVSpeechSynthesisVoice(identifier: AVSpeechSynthesisVoiceIdentifierAlex), but oddly the only identifier that Apple have provided to date is for Alex. 🤷

For any other voices (see the above list again for the voice names), use the following code:

// 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
        }
    } 
}

(This could be done more elegantly using filter() than a loop, but I'm not familiar with the syntax in Swift, so refer here if interested, or leave a comment if you know how to write it).

As for the voices labelled 'Siri', I have no idea. Maybe they're unrelated to AVSpeechSynthesisVoice? Comments welcomed if you know the deal behind them.




回答2:


Following up on Jamie answer (now iOS 11.3 I believe also works with earlier versions):

for the other voices the identifier is structured as "com.apple.ttsbundle.Kanya-compact"

You can get those that are available by:

let voices = AVSpeechSynthesisVoice.speechVoices()
for voice in voices {
    print (voice.identifier + " " + s.language + " " + s.name)
}

and set the voice by either identifier or language.

//set utterance first!
speechUtter = AVSpeechUtterance(string:"أتمنى لك نهارا سعيد")

speechUtter.voice = AVSpeechSynthesisVoice(identifier: "com.apple.ttsbundle.Maged-compact") 

or

speechUtter.voice = AVSpeechSynthesisVoice(language: "ar-SA")



回答3:


You can use this simple command print(AVSpeechSynthesisVoice.speechVoices())



来源:https://stackoverflow.com/questions/35492386/how-to-get-a-list-of-all-voices-on-ios-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!