Search code examples
ioslocalizationtext-to-speech

AVSpeechSynthesizer: AVSpeechSynthesisIPANotationAttribute seems like does not support Chinese


When testing AVSpeechSynthesizer, AVSpeechSynthesisIPANotationAttribute seems like does not support Chinese?

en-GB is OK:

let dst = NSMutableAttributedString(string: "It's pronounced 'tomato'")
let pronunciationKey = NSAttributedString.Key(rawValue: AVSpeechSynthesisIPANotationAttribute)
let range = NSString(string: "It's pronounced 'tomato'").range(of: "tomato")
// just test a joke
dst.setAttributes([pronunciationKey: "tə.do͡ʊ.ˈme͡ɪ"], range: range)
let result = dst.copy() as! NSAttributedString
utterance = AVSpeechUtterance(attributedString: result)
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
synthesizer.speak(utterance)

zh-CN is not OK

let dst = NSMutableAttributedString(string: "行人")
let pronunciationKey = NSAttributedString.Key(rawValue: AVSpeechSynthesisIPANotationAttribute)
let range = NSString(string: "行人").range(of: "行")
dst.setAttributes([pronunciationKey: "ˈme͡ɪ"], range: range)
//  I also tried "a"
//  dst.setAttributes([pronunciationKey: "a"], range: range)
let result = dst.copy() as! NSAttributedString
utterance = AVSpeechUtterance(attributedString: result)
utterance.voice = AVSpeechSynthesisVoice(language: "zh-CN")
synthesizer.speak(utterance)

Neither is zh-HK, zh-TW.

Perhaps there is some low level solution.


Solution

  • According to the WWDC 2018 Session "AVSpeechSynthesizer: Making iOS Talk", IPA Notation is only available for the following languages:

    en-US, en-AU, en-GB, de-DE, es-ES, es-MX, fr-CA, fr-FR, it-IT, ja-JP

    I couldn't find any newer reference nor any other official documentation by Apple for AVSpeechSynthesisIPANotationAttribute.

    Sources:
    https://developer.apple.com/videos/play/wwdc2018/236/?time=424 https://nshipster.com/avspeechsynthesizer/