Arun kumar.c
Arun kumar.c

Reputation: 69

IOS Swift : UIAccessibilitySpeechAttributeIPANotation

I am trying to make my app to speak in the IPANotation using AVSpeechSynthesizer, but it not working below is my code snippet,

let synthesizer = AVSpeechSynthesizer()

let attributedStr = NSMutableAttributedString(string: "Hello iPhone")
attributedStr.addAttribute(NSAttributedString.Key(UIAccessibilitySpeechAttributeIPANotation), value: "ˈa͡ɪ.ˈfo͡ʊn", range: NSRange(location: 6, length: 6))

let utterance = AVSpeechUtterance(attributedString: attributedString)
synthesizer.speak(utterence)

Thanks

Upvotes: 2

Views: 735

Answers (1)

XLE_22
XLE_22

Reputation: 5671

Try the code snippet hereafter (iOS 12 - Swift 5.0) :

let attrStr = NSMutableAttributedString(string: "hello my little ")

let pronunciationKey = NSAttributedString.Key(rawValue: AVSpeechSynthesisIPANotationAttribute)
let attrStr2 = NSMutableAttributedString(string: "blablabla",
                                         attributes: [pronunciationKey: "ˈa͡ɪ.ˈfo͡ʊn"])

let finalAttrStr = NSMutableAttributedString()
finalAttrStr.append(attrStr)
finalAttrStr.append(attrStr2)

let utterance = AVSpeechUtterance(attributedString: finalAttrStr)
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")

let synthesizer = AVSpeechSynthesizer()
synthesizer.speak(utterance)

This is not perfect but it makes the IPA notation work using AVSpeechSynthesizer.

Another example is also available to figure out how to get the phonemes and put them into the code.

Upvotes: 1

Related Questions