AVSpeechSynthesis
API for computer synthesized speech
Users
- Announcements
- Non-sighted interfaces
- Education apps
- Many more…
AVSpeechSynthesis and Accessibility
Powerful tool for helping many users
- Cognitive
- Speech vocalization
- Non-sighted
Not a replacement for VoiceOver
- Speech can overlap with VoiceOver’s
- Won’t be available to Braille devices
- Make your app accessible instead using UIAccessibility
Create AVSpeechSynthesizer
Ensure it’s retained until speech is done
(Speech will be cancelled in the synthesizer is deallocated)
let synthesizer = AVSpeechSynthesizer()
//Create an utterance
let utterance = AVSpeechUtterance(string: contentTextView.text)
//Dispatch to synthesizer
synthesizer.speak(utterance)
AVSpeechSynthesis and Audio Sessions
//AVAudioSession automatically activated on speak()
//To mix with other audio, use
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode:.spokenAudio, options:.mixWithOthers)
} catch {
print("error")
}
//To duck other audio, use
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode:.spokenAudio, options:.duckOthers)
} catch {
print("error")
}
Callbacks
Delegate methods inform about the life cycle of an utterance
AVSpeechSynthesizerDelegate defines optional methods
- Speech started
- Speech finished
- Character range will be spoken
- Speech paused
- Speech continued
synthesizer.delegate = self
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didStart utterance: AVSpeechUtterance)
{
print("Speech started")
}
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance:
AVSpeechUtterance) {
print("Speech finished")
}
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString
characterRange: NSRange, utterance: AVSpeechUtterance) {
guard let rangeInString = Range(characterRange, in: utterance.speechString()) else { return }
print("Will speak: \(utterance.speechString[rangeInString])")
}
Choosing the Right Voice
Select with an identifier or a language
Selecting by language will select the users default voice
let utterance = AVSpeechUtterance(string: "Hello")
// Select an English (US) voice preferred by user (if no preference, default is used)
utterance.voice = AVSpeechSynthesisVoice(language: "en-US”)
// Select the first voice
let allVoices = AVSpeechSynthesisVoice.speechVoices()
utterance.voice = AVSpeechSynthesisVoice(identifier: allVoices[0].identifier)
Languages supported
Arabic <阿拉伯>
Cantonese(Hong Kong)<廣東話.香港> zh-hk
Czech<捷克語>
Danish <丹麥語>
Dutch <荷蘭語>
English (US) <英語.美國> en-us
English (UK)<英語.英國>
English (Australia) <英語.澳大利亞>
English (Ireland) <英語.愛爾蘭>
English (South Africa) <英語.南非>
Finnish <芬蘭語>
Flemish (Belgium) <佛蘭德.比利時>
French(France) <法語.法國>
French (Canada) <法語.加拿大>
German <德語>
Greek <希臘語>
Hebrew <希伯來語>
Hindi<北印度語>
Hungarian<匈牙利語>
Indonesian <印尼語>
Italian <意大利語>
Japanese <日語>
Korean <韓國語>
Mandarin (Mainland China) <普通話.大陸> zh-cn
Mandarin(Taiwan) <普通話.臺灣>
Norwegian <挪威語>
Polish <波蘭語>
Portuguese <葡萄牙語>
Portuguese (Brazil) <葡萄牙語.巴西>
Romanian <羅馬尼亞語>
Russian <俄語>
Slovak <斯洛伐克語>
Spanish (Mexico) <西班牙語.墨西哥>
Spanish (Spain) <西班牙語.西班牙>
Swedish <瑞典語>
Thai <泰國語>
Turkish <土耳其語>
Speech Rate
utterance.pitchMultiplier = 1 // [0.5 - 2] Default = 1
utterance.volume = 0.25 // lower speech volume, does not affect system volume [0-1] Default = 1
utterance.rate = AVSpeechUtteranceDefaultSpeechRate // Values are pinned between AVSpeechUtteranceMinimumSpeechRate and AVSpeechUtteranceMaximumSpeechRate.
IPA Notation
Use attributed string API with IPA pronunciation
let attributedString = NSMutableAttributedString(string: "Hello iPhone") attributedString.addAttribute(.accessibilitySpeechIPANotation, value: "ˈa͡ɪ.ˈfo͡ʊn", range: NSRange(location: 6, length: 6)))
let utterance = AVSpeechUtterance(attributedString: attributedString)