7

I am using AVSpeechSynthesizer for text-to-speech in my app. Is it possible to stop the playback in the middle, e.g. if the user hits a stop button?

Jason
  • 14,517
  • 25
  • 92
  • 153

2 Answers2

15

Looking at the Documentation for AVSpeechSynthesizer, you need to call -[AVSpeechSynthesizer pauseSpeakingAtBoundary:] to stop speaking, and, then -[AVSpeechSynthesizer continueSpeaking] to continue speaking again afterwards.

You can choose to pause speaking right away (AVSpeechBoundaryImmediate), or, at the end of the current word (AVSpeechBoundaryWord).

shim
  • 9,289
  • 12
  • 69
  • 108
zadr
  • 2,505
  • 18
  • 19
  • Hello, I can't Stop on any button tapped, I want to stop it when cancel button pressed. I tried as below: if(av_speechSynthesizer.isSpeaking || av_speechSynthesizer){ do something } – Er.Shreyansh Shah Nov 05 '14 at 11:35
  • 3
    Instead of "pauseSpeakingAtBoundary:" use [AVSpeechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate]; – Er.Shreyansh Shah Nov 05 '14 at 11:41
9

In swift:

synthesizer.stopSpeaking(at: .immediate)

or

synthesizer.stopSpeaking(at: .word)
Hemang
  • 26,840
  • 19
  • 119
  • 186