Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVSpeechSynthesizer delegates are not calling in iOS 11

Here is my code for text-to-speech. Everything is working fine below iOS 11. But in iOS 11 delegate methods are not being called.

AVSpeechSynthesizerDelegate is set accurately. (As it is working below iOS 11)

On button tap

AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];
synthesizer.delegate = self;

AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:stringToSpeech];
[utterance setRate:AVSpeechUtteranceDefaultSpeechRate];
[utterance setPitchMultiplier:1];
[utterance setVolume:1];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
[synthesizer speakUtterance:utterance];

These are delegate methods I have implemented.

-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"finished");
}

-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance{
    NSLog(@"Started");
}

Did someone face this issue? Any help will be greatly appreciated.

I m testing on iOS 11.0.3, with Xcode 9.0

like image 552
M Zubair Shamshad Avatar asked Jan 04 '23 08:01

M Zubair Shamshad


1 Answers

Your synthesizer object must be a property for that to work :).

Either make your AVSpeechSynthesizer instance, which is synthesizer like so:

@property (strong, nonatomic) AVSpeechSynthesizer *synthesizer;

or like this with readwrite.

@property (readwrite, strong, nonatomic) AVSpeechSynthesizer *synthesizer;

Let me know if this works.

like image 158
Glenn Posadas Avatar answered Jan 15 '23 20:01

Glenn Posadas