Use `AVSpeechSynthesizer` with `MPRemoteCommandCenter`

Hi,

I need to use AVSpeechSynthesizer to read a text to my user.

User will control reading with AirPods, so I need to use MPRemoteCommandCenter.

For now, I need to prepare my audio files using AVSpeechSynthesizer.write(_:toBufferCallback:) the create a playlist and read it using AVQueuePlayer.

It works. But preparing audio files take time. I prefer to use AVSpeechSynthesizer.speak(_:) directly in background mode, and activate it via MPRemoteCommandCenter commands.

Is this possible? Or perhaps any workaround?

Thank you!

Replies

I don't known AVSpeechSynthesizer much, maybe what I said was not accurate.
I found that MPRemoteCommandCenter did only work after AVSpeechSynthesizer first spoke something, so I though maybe a workaround was invoking AVSpeechSynthesizer with speaking an empty word after the preparation had done.
Code Block
let utterance = AVSpeechUtterance(string: "") // notice that initializing AVSpeechUtterance with empty string
utterance.rate = 0.5
utterance.pitchMultiplier = 0.8
utterance.postUtteranceDelay = 0.2
utterance.volume = 0.8
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")!
speechSynthesizer.speak(utterance)

After that, your MPRemoteCommandCenter may take control.