Skip to content

Instantly share code, notes, and snippets.

@samdods
Created October 9, 2019 10:11
Show Gist options
  • Save samdods/340f6f47a5a0ff2213d77726f07879a7 to your computer and use it in GitHub Desktop.
Save samdods/340f6f47a5a0ff2213d77726f07879a7 to your computer and use it in GitHub Desktop.
Showing AVSpeechSynthesizer extension for Speak Selection blog post
extension AVSpeechSynthesizer {
static let forSpeakSelection: AVSpeechSynthesizer = {
let synthesizer = AVSpeechSynthesizer()
synthesizer.delegate = SpeechDelegate.shared
return synthesizer
}()
}
// MARK: - Private
/// Delegate is used to deactivate the audio session when a synthesized utterance has finished.
private class SpeechDelegate: NSObject, AVSpeechSynthesizerDelegate {
static let shared = SpeechDelegate()
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
try? AVAudioSession.sharedInstance().setActive(false)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment