DEV Community

Kelvin Ng
Kelvin Ng

Posted on • Updated on

AVSpeechSynthesisVoice Fall Back Mechanism

I recently received complaints from my users stating that some (but not all) devices couldn't perform TTS in my dictionary app.

At first, I was quite confused because all the devices I had on hand worked fine, as did all the simulators.

Eventually, I realized that the issue might be caused by missing target voices on the user's device.

This can be resolved by implementing a fallback mechanism if the user hasn't installed the required voice package.

// specific target voice
let targetVoiceID = ""

// fallback language
let fallbackLang = "zh-TW"

// fallback if the user doesn't have the target voice
let voice = AVSpeechSynthesisVoice(identifier: targetVoiceID) ?? AVSpeechSynthesisVoice(language: fallbackLang)
Enter fullscreen mode Exit fullscreen mode

One notable feature of AVSpeechSynthesisVoice(language:) is that it will further fallback to the system's default voice if the target language is not available. In contrast, AVSpeechSynthesisVoice(identifier:) will simply return nil.

On reflection, I probably should invite users who are experiencing issues to become beta testers, even though we've only connected online and have never met in person. 😜


Top comments (0)