测试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 session “AVSpeechSynthesizer:让 iOS 说话”, 国际音标表示法仅适用于以下语言:
en-US、en-AU、en-GB、de-DE、es-ES、es-MX、fr-CA、fr-FR、it-IT、ja-JP
我找不到任何更新的引用资料,也找不到 Apple 提供的有关 AVSpeechSynthesisIPANotationAttribute 的任何其他官方文档。
来源:
https://developer.apple.com/videos/play/wwdc2018/236/?time=424
https://nshipster.com/avspeechsynthesizer/
关于ios - AVSpeech合成器: AVSpeechSynthesisIPANotationAttribute seems like does not support Chinese,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58225479/