
The AVSpeechSynthesizer class introduced from iOS7 produces synthesized speech from text on an iOS device, and provides methods for controlling or monitoring the progress of ongoing speech.
This class is a part of the powerful AVFoundation framework.
This class is responsible for carrying out the heavy work of converting text to speech. It’s capable of initiating, pausing, stopping and continuing a speech process.
AVSpeechUtterance is an intermediate class that interact directly with the text.
Properties of AVSpeechUtterance class:
1. rate : Rate at which the utterance will be spoken.
- Type : Float
- Range from AVSpeechUtteranceMinimumSpeechRate and AVSpeechUtteranceMaximumSpeechRate.
- Lower value for slow speech and higher value for vice versa.
2. pitchMultiplier : The Pitch at which the utterance will be spoken.
- Type : Float
- Default value – 1.0
- Allowed values – 0.5 (lower pitch) to 2.0 (higher pitch).
3. volume : Volume to be used when speaking.
- Type : Float
- Default value – 1.0
- Allowed values – 0.0(silent) to 1.0(loudest)
4. speechString : Text to be spoken in the utterance.
- Type : String
Note : Utterance text cannot be changed once it is created. To Speak different, create a new utterance.
5. voice : The voice used to speak the utterance
- Type : AVSpeechSynthesisVoice class
- Default value is nil speaks in default voice
6. preUtteranceDelay : The amount of time a speech synthesiser will wait before actually speaking the text.
- Type : NSTimeInterval
7. postUtteranceDelay : The amount of time a speech synthesiser will wait after actually speaking the text.
- Type : NSTimeInterval
Properties of AVSpeechSynthesizer class :
1. speaking : A Boolean value that indicates whether the synthesiser is speaking which is readonly.
2. paused : A Boolean value that indicates whether speech has been paused which is readonly.
Methods of AVSpeechSynthesizer class :
1. func speakUtterance(utterance: AVSpeechUtterance) :
- Enqueues i.e add an utterance to be spoken to the queue.
- Calling this method adds the utterance to a queue.
- Utterances are spoken in the order in which they are added to the queue
Note : Attempting to enqueue AVSpeechUtterance multiple times throws an exception
2. func stopSpeakingAtBoundary(boundary: AVSpeechBoundary) -> Bool
- Stops all speech at the specified boundary constraint
- Boundary values can be Immediate or word
- Immediate – Indicates that speech should pause or stop immediately
- word – Indicates that speech should pause or stop after word currently being spoken
- Returns true if speech has stopped or false otherwise
3. func pauseSpeakingAtBoundary(boundary: AVSpeechBoundary) -> Bool :
- Pauses all speech at the specified boundary constraint
- Returns true if speech has paused or false otherwise
4. func continueSpeaking() -> Bool :
- Continues speech from the point at which it left off
- Returns true if speech has continued or false otherwise
Delegate methods of AVSpeechSynthesizer class :
All the delegate methods of AVSpeechSynthesizer class are optional.
1. func speechSynthesizer(synthesizer: AVSpeechSynthesizer, didStartSpeechUtterance utterance: AVSpeechUtterance) :
This tells the delegate when the synthesizer has began speaking an utterance
2. func speechSynthesizer(synthesizer: AVSpeechSynthesizer, didFinishSpeechUtterance utterance: AVSpeechUtterance) :
This tells the delegate when the synthesizer has finished speaking an utterance.
3. func speechSynthesizer(synthesizer: AVSpeechSynthesizer, didPauseSpeechUtterance utterance: AVSpeechUtterance) :
This tells the delegate when the synthesizer has paused while speaking an utterance.
4. func speechSynthesizer(synthesizer: AVSpeechSynthesizer, didContinueSpeechUtterance utterance: AVSpeechUtterance) :
This tells the delegate when the synthesizer has resumed speaking an utterance after being paused.
5. fun speechSynthesizer(synthesizer: AVSpeechSynthesizer, didCancelSpeechUtterance utterance: AVSpeechUtterance) :
This tells the delegate when the synthesizer has canceled speaking an utterance.
6. func speechSynthesizer(synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) :
This tells the delegate when the synthesizer is about to speak a portion of an utterance’s text.
Sample Code :
let synthesizer = AVSpeechSynthesizer()
var speechUtterance = AVSpeechUtterance(string: “”)
speechUtterance = AVSpeechUtterance(“Your String”)
speechUtterance.rate = 0.3
speechUtterance.pitchMultiplier = 0.25
speechUtterance.volume = 0.75
speechUtterance.postUtteranceDelay = 0.001
speechUtterance.voice = AVSpeechSynthesisVoice(language: “en-GB”)
synthesizer.speakUtterance(speechUtterance)
Supported languages for the voice:
As of iOS 8.1, [AVSpeechSynthesisVoice speechVoices] the following languages and locales are supported:
- Arabic (Saudi Arabia) – ar-SA
- Chinese (China) – zh-CN
- Chinese (Hong Kong SAR China) – zh-HK
- Chinese (Taiwan) – zh-TW
- Czech (Czech Republic) – cs-CZ
- Danish (Denmark) – da-DK
- Dutch (Belgium) – nl-BE
- Dutch (Netherlands) – nl-NL
- English (Australia) – en-AU
- English (Ireland) – en-IE
- English (South Africa) – en-ZA
- English (United Kingdom) – en-GB
- English (United States) – en-US
- Finnish (Finland) – fi-FI
- French (Canada) – fr-CA
- French (France) – fr-FR
- German (Germany) – de-DE
- Greek (Greece) – el-GR
- Hindi (India) – hi-IN
- Hungarian (Hungary) – hu-HU
- Indonesian (Indonesia) – id-ID
- Italian (Italy) – it-IT
- Japanese (Japan) – ja-JP
- Korean (South Korea) – ko-KR
- Norwegian (Norway) – no-NO
- Polish (Poland) – pl-PL
- Portuguese (Brazil) – pt-BR
- Portuguese (Portugal) – pt-PT
- Romanian (Romania) – ro-RO
- Russian (Russia) – ru-RU
- Slovak (Slovakia) – sk-SK
- Spanish (Mexico) – es-MX
- Spanish (Spain) – es-ES
- Swedish (Sweden) – sv-SE
- Thai (Thailand) – th-TH
- Turkish (Turkey) – tr-TR
Thus Text to Speech feature is made easier with AVSpeechSynthesizer class. This TTS feature can do even more to help blind or disabled users to make the text messages to speak. You can test this feature in simulator itself which adds to its advantage. It only takes five to six lines of code to add speech to your app!.