Does AVSpeechSynthesizer Work In App Extensions?

258 views Asked by At

Is it possible to use AVSpeechSynthesizer in an app extension?

I've added an app app action extension to my app, and on the default controller that is shown, I've added some test code in the - (void)viewDidAppear:(BOOL)animated hook to see if I can get the speech synthesizer to say @"Hello World" But it doesn't seem to make any sounds.

I then tried setting the synthesizer's delegate to the controller, and all seems to be working as well.

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance

gets called. followed a few seconds later by

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance

This seems to show that things are working correctly, but no sound is outputted (my volume is up).

I tried adding the same snippet of code to my app (the containing app) and it speaks the texts as expected.

I understand some frameworks are not available to app extensions, as stated in the documentation. But AVFoundation doesn't seem to state anything about it not being available to app extensions.

Any insights would be appreciated.

Thanks in advance!

AVSpeechSynthesizer *s = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *u = [[AVSpeechUtterance alloc] initWithString:@"Hello World!"];
[s speakUtterance:u];
1

There are 1 answers

0
Ben On

Yes it does. I have a action extension that works with safari and speaks the content of the website using AVSpeechSynthesizer, and the strange thing is that I didn't even enable background audio for the app when I tried. The extension was activated successfully and started reading the content of the DOM.

One thing you do have to be careful is sharing data between the container app and the extension. You'll need to have