Search code examples
iosswiftavfoundationavspeechsynthesizeravspeechutterance

AVSpeechSynthesizer: how to display in a default player view


I use AVSpeechSynthesizer to play text books via audio.

private lazy var synthesizer: AVSpeechSynthesizer = {
    let synthesizer = AVSpeechSynthesizer()
    synthesizer.delegate = self
    return synthesizer
  }()

let utterance = AVSpeechUtterance(string: text)
    utterance.voice = AVSpeechSynthesisVoice(
      language: languageIdentifier(from: language)
    )
    synthesizer.speak(utterance)

I want to update information in iPhone's default player view (probably naming is wrong 🙏):

enter image description here

  • indicate playing Chapter with some text
  • enable next button to play the next chapter

How can I accomplish this?


Solution

  • I really don't think you want to hack your way through this.. But if you really do I would:

    • Listen to remote commands (UIApplication.sharedApplication().beginReceivingRemoteControlEvents(), see Apple Sample Project
    • Set your properties on MPNowPlayingInfoCenter: MPNowPlayingInfoCenter.default().nowPlayingInfo[MPMediaItemPropertyTitle] = "Title"
    • Implement the AVSpeechSynthesizerDelegate and try to map the delegate functions to playback states and estimate the playback progress using speechSynthesizer(_:willSpeakRangeOfSpeechString:utterance:) (idk if possible)
    • You might have to play with the usesApplicationAudioSession property of AVSpeechSynthesizer to have more control over the audio session (set categories etc.)