AVSpeechSynthesizer

text-to-audio-gig2The 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:

 i2i_holagraphic

As of iOS 8.1, [AVSpeechSynthesisVoice speechVoices] the following languages and locales are supported:

  1. Arabic (Saudi Arabia) – ar-SA
  2. Chinese (China) – zh-CN
  3. Chinese (Hong Kong SAR China) – zh-HK
  4. Chinese (Taiwan) – zh-TW
  5. Czech (Czech Republic) – cs-CZ
  6. Danish (Denmark) – da-DK
  7. Dutch (Belgium) – nl-BE
  8. Dutch (Netherlands) – nl-NL
  9. English (Australia) – en-AU
  10. English (Ireland) – en-IE
  11. English (South Africa) – en-ZA
  12. English (United Kingdom) – en-GB
  13. English (United States) – en-US
  14. Finnish (Finland) – fi-FI
  15. French (Canada) – fr-CA
  16. French (France) – fr-FR
  17. German (Germany) – de-DE
  18. Greek (Greece) – el-GR
  19. Hindi (India) – hi-IN
  20. Hungarian (Hungary) – hu-HU
  21. Indonesian (Indonesia) – id-ID
  22. Italian (Italy) – it-IT
  23. Japanese (Japan) – ja-JP
  24. Korean (South Korea) – ko-KR
  25. Norwegian (Norway) – no-NO
  26. Polish (Poland) – pl-PL
  27. Portuguese (Brazil) – pt-BR
  28. Portuguese (Portugal) – pt-PT
  29. Romanian (Romania) – ro-RO
  30. Russian (Russia) – ru-RU
  31. Slovak (Slovakia) – sk-SK
  32. Spanish (Mexico) – es-MX
  33. Spanish (Spain) – es-ES
  34. Swedish (Sweden) – sv-SE
  35. Thai (Thailand) – th-TH
  36. 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!.

Bhavani,
iOS Developer,
Mallow Technologies.

Leave a Reply

%d bloggers like this: