AVSpeechSynthesizer stops working after background recording

I am using AVSpeechSynthesizer in singleton mode. In iOS 8 , when the application receives some time, when it resumes the AVSpeechSynthesizer singleton, it will no longer speak. This issue does not occur with iOS 7 .

When the application receives background messages, the following message appears in my log:

 AVSpeechSynthesizer Audio interruption notification: { AVAudioSessionInterruptionTypeKey = 1; } 

I initialize AVSpeechSynthesizer as follows in the singleton init method:

  self.speechSynthesizer = [[AVSpeechSynthesizer alloc] init]; self.speechSynthesizer.delegate = self; 

and I say utterance as follows:

 AVSpeechUtterance *utt = [[AVSpeechUtterance alloc] initWithString:dialogue]; utt.voice = [AVSpeechSynthesisVoice voiceWithLanguage:voice]; utt.pitchMultiplier = pitch; utt.rate = rate; utt.preUtteranceDelay = preDelay; utt.postUtteranceDelay = postDelay; utt.volume = volumeSetting; [self.speechSynthesizer speakUtterance:utt]; 

Has anyone seen something similar on iOS 8 ?

+7
ios ios8 avspeechsynthesizer
source share
3 answers

I spent the whole day chasing this madness, and I think I found a solution. My problem was that AVSpeechSynthesizer will work in the foreground and in the background with a bite of another sound right up to the time the phone is called.

At this point, the speech will stop working silently, without any errors. All objects still exist, but delegate calls will not be called, neither begin nor end.

I noticed that with the help of phone calls, my application will receive a notification about AudioRouteChanged . Thus, when this happens, I would recreate the speech setup: basically destroy the existing AVSpeechSynthesizer and create it again. From this point on, the conversation will continue. This will work even during a phone call :)

+12
source share
  • You must install "Audio and AirPlay" in the background.
  • You need to set up an audio session in AppDelegate:

     NSError *error = NULL; AVAudioSession *session = [AVAudioSession sharedInstance]; [session setCategory:AVAudioSessionCategoryPlayback error:&error]; if(error) { // Do some error handling } [session setActive:YES error:&error]; if (error) { // Do some error handling } 

(See this post: https://stackoverflow.com/a/312947/ )

+2
source share

After further study, it seems that AVSpeechSynthesizer breaks down when the application starts in the background (due to background fetching or something else). A simple call to check if the application is really active, before speaking, solves the problem.

 if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) [self.speechSynthesizer speakUtterance:utt]; 
0
source share

All Articles