测试 AVSpeechSynthesizer 时,AVSpeechSynthesisIPANotationAttribute
好像不支持中文?
en-GB 是可以的:
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 不行
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)
zh-HK,zh-TW也不是。
也许有一些低级的解决方案。
根据WWDC 2018会议"AVSpeechSynthesizer:Making iOS Talk", 国际音标表示法仅适用于以下语言:
en-us, en-au, en-GB, de-DE, es-ES, es-MX, fr-CA, fr-FR, it-IT, ja-JP
我找不到任何更新的参考资料,也找不到苹果关于AVSpeechSynthesisIPANotationAttribute的任何其他官方文档。
来源:https://developer.apple.com/videos/play/wwdc2018/236/?time=424 https://nshipster.com/avspeechsynthesizer/