My iOS app provides AVSpeechSynthesizer
support in multiple languages (not just the device's current language).
In Settings->General->Accessibility->Speech->Voices->Spanish
, the user can set a preferred voice to either "Spanish (Spain)" or "Spanish (Mexico)" (which correspond to es-ES and es-MX, respectively).
Similarly with Portuguese (pt-PT, pt-BR), French (fr-FR, fr-CA), etc.
If one of my users wants to have my app speak a Spanish phrase, how can I determine which of the available Spanish voices the user has selected in Settings?
I tested [AVSpeechSynthesisVoice speechVoices]
to see whether the ordering of the voices changes when user preferences changes, but the answer appeared to be no. [AVSpeechSynthesisVoice voiceWithLanguage:]
requires the full language-locale string, so passing in "es" gets you nil even though two Spanish voices are available by default.
I scanned through all the keys in [NSUserDefaults standardUserDefaults]
, but there's nothing relevant there. And I've looked through the UIAccessibility
documentation without finding what I need.
The approach the works for me is as follows:
let defVoice = AVSpeechSynthesisVoice(language: NSLocale.current.languageCode)
I've tested in on English, of which there are many variants. It returns the voice that the user has selected in Settings as their preferred voice for their current locale.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With