我正在尝试将文本到语音功能集成到我的 iOS 应用程序中。
为此我正在使用AVSpeechUtterance
and AVSpeechSynthesisVoice
类的AVFoundation
框架。
extension String {
func speech(with pronunciation: String) {
let utterance = AVSpeechUtterance(attributedString: NSAttributedString(string: self, attributes: [.accessibilitySpeechIPANotation : pronunciation]))
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
let synth = AVSpeechSynthesizer()
DispatchQueue.main.async {
synth.speak(utterance)
}
}
}
我面临的问题是发音wind单词作为动词和名词,即
- 风作为verb发音为:waɪnd
- 和风作为noun发音为:wɪnd
上述发音字符串遵循国际音标 (IPA).
但是,我没有得到预期的结果。
如果您想要特定拼写的 IPA 翻译,我建议使用位于以下位置的 iOS 功能:
-
Settings > General > Accessibility > Speech > Pronunciations
(iOS 12).
-
Settings > Accessibility > Spoken Content > Pronunciations
(iOS 13)
选择所需的结果后,您可以在你的代码中使用它 https://a11y-guidelines.orange.com/mobile_EN/dev-ios.html#speech-synthesis由语音合成器发声。
EDIT
这个解决方案也不适合我。
我对您的评论感到非常惊讶,因为当我按照提供的链接的每个步骤操作时,我得到了下面的代码片段:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let pronunciationKey = NSAttributedString.Key(rawValue: AVSpeechSynthesisIPANotationAttribute)
// let attrStr = NSMutableAttributedString(string: "blablablaNOUN",
// attributes: [pronunciationKey: "ˈwɪnd"])
let attrStr = NSMutableAttributedString(string: "blablablaVERB",
attributes: [pronunciationKey: "ˈwa͡ɪnd"])
let utterance = AVSpeechUtterance(attributedString: attrStr)
let synthesizer = AVSpeechSynthesizer()
synthesizer.speak(utterance)
}
...当我启动这个空白应用程序时更改后iPhone Language
in the Settings - General - Language & Region
菜单中,我得到了动词和名词的正确发音。
复制粘贴上面的代码片段并自行测试。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)