Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

determining user's preferred AVSpeechSynthesisVoice for a given language

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.

like image 320
Jeff Ondich Avatar asked Sep 19 '25 12:09

Jeff Ondich


1 Answers

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.

like image 90
Jack Cox Avatar answered Sep 22 '25 09:09

Jack Cox