vijayst vijayst - 1 month ago 9x
iOS Question

An issue with AVSpeechSynthesizer, Any workarounds?

I am using AVSpeechSynthesizer to play text. I have an array of utterances to play.

NSMutableArray *utterances = [[NSMutableArray alloc] init];
for (NSString *text in textArray) {
AVSpeechUtterance *welcome = [[AVSpeechUtterance alloc] initWithString:text];
welcome.rate = 0.25;
welcome.voice = voice;
welcome.pitchMultiplier = 1.2;
welcome.postUtteranceDelay = 0.25;
[utterances addObject:welcome];
lastUtterance = [utterances lastObject];
for (AVSpeechUtterance *utterance in utterances) {
[speech speakUtterance:utterance];

I have a cancel button to stop speaking. When I click the cancel button when the first utterance is spoken, the speech stops and it clears all the utterances in the queue. If I press the cancel button after the first utterance is spoken (i.e. second utterance), then stopping the speech does not flush the utterances queue. The code that I am using for this is:

[speech stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];

Can someone confirm if this is a bug in the API or am I using the API incorrectly? If it is a bug, is there any workaround to resolve this issue?


quite likely to be a bug, in that the delegate method synthesizer didCancelSpeechUtterance isn't called after the first utterance;

A workaround would be to chain the utterances rather than have them in an array and queue them up at once.

Use the delegate method synthesizer didFinishSpeechUtterance to increment an array pointer and speak the the next text from that array. Then when trying to stop the speech, set a BOOL that is checked in this delegate method before attempting to speak the next text.

For example:

1) implement the protocol in the view controller that is doing the speech synthesis

#import <UIKit/UIKit.h>
@import AVFoundation;
@interface ViewController : UIViewController <AVSpeechSynthesizerDelegate>


2) instantiate the AVSpeechSynthesizer and set its delegate to self

speechSynthesizer   = [AVSpeechSynthesizer new];
speechSynthesizer.delegate = self;

3) use an utterance counter, set to zero at start of speaking

4) use an array of texts to speak

textArray           = @[@"Mary had a little lamb, its fleece",
                        @"was white as snow",
                        @"and everywhere that Mary went",
                        @"that sheep was sure to go"];

5) add delegate method didFinishSpeechUtterance to speak the next utterance from the array of texts and increment the utterance counter

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance{
    if(utteranceCounter < utterances.count){
        AVSpeechUtterance *utterance = utterances[utteranceCounter];
        [synthesizer speakUtterance:utterance];

5) to stop speaking, set the utterance counter to the count of the texts array and attempt to get the synthesizer to stop

utteranceCounter = utterances.count;

BOOL speechStopped =  [speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    [speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryWord];

6) when speaking again, reset the utterance counter to zero