Search code examples
swifttext-to-speechsiriavspeechsynthesizer

Set language for speech utterance in Swift 2024


The old way to set the language for a speech utterance in Swift for text to speech was:

let utterance = AVSpeechUtterance(string: "Hello world")
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
utterance.rate = 0.1 

let synthesizer = AVSpeechSynthesizer()
synthesizer.speak(utterance)

there was one voice per country.

However Apple has since added more than one voice per country, male and female voices and custom voices. There is an Alex, Samantha etc. Users can create their own voice. Apple has introduced Voiceovers and a lot of the voice questions seem to now fall under accessibility. Although Apple is marketing the ability for users to select new voices, the documentation does not seem to give a straightforward way to access the wider range of voices programatically.

What apis are you supposed to use to programatically access these new voices in 2024?

Thanks for any suggestions.


Solution

  • You are supposed to go through list of speechVoices() and find a voice you want. You can find a desired voice by language, quality, gender etc. Check these properties as you loop through the list.

    It is reasonable that there is no API to specifically get, say, "Samantha". Apple is free to remove it in future versions this way., or for some voices, the user might not even have it downloaded, and your app is not supposed to download it for the user. Technically you can find the identifier of a specific voice you want and use init(identifier:), but that initialiser could very well fail. The only stable identifier is Alex.

    Another common pattern is to display the list of voices to the user and let them pick.