Skip to content

Instantly share code, notes, and snippets.

View myrickchow32's full-sized avatar

Myrick Chow myrickchow32

  • RedSo
  • Hong Kong
View GitHub Profile
class AVFoundataionDemoViewController: BaseViewController {
let avSpeechSynthesizer = AVSpeechSynthesizer()
override func viewDidLoad() {
super.viewDidLoad()
avSpeechSynthesizer.delegate = self
}
}
extension AVFoundataionDemoViewController: AVSpeechSynthesizerDelegate {
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didStart utterance: AVSpeechUtterance) {
var avSpeechSynthesizer: AVSpeechSynthesizer!
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
avSpeechSynthesizer.stopSpeaking(at: .immediate)
}
class AppDelegate: UIResponder, UIApplicationDelegate {
func applicationDidEnterBackground(_ application: UIApplication) {
// AVSpeechSynthesizer will be automatically stopped with a smooth volume reduction
// The parameter of stopping the speech is AVSpeechBoundary.immediate
print("applicationDidEnterBackground")
}
func applicationDidBecomeActive(_ application: UIApplication) {
// AVSpeechSynthesizer will be automatically resumed
print("applicationDidBecomeActive")
// Utterance contains with both English and Traditional Chinese words
let utterance = AVSpeechUtterance(string: "Hong Kong(香港)is in Asia.")
utterance.voice = AVSpeechSynthesisVoice(language: "zh-HK")
let avSpeechSynthesizer = AVSpeechSynthesizer()
avSpeechSynthesizer.speak(utterance)
/*
Three possible outcomes:
1. The user selected accent in "en-AU" category
2. iOS default accent in "en-AU" category
2.1 Compact
2.2 Enhanced (if user has downloaded)
*/
let voice = AVSpeechSynthesisVoice(language: "en-AU")
func getUtteranceWithDelay(preUtteranceDelayInSecond: Int, postUtteranceDelayInSecond: Int) -> AVSpeechUtterance {
let utterance = AVSpeechUtterance(string: "Hello world")
utterance.preUtteranceDelay = TimeInterval.init(exactly: preUtteranceDelayInSecond)!
utterance.postUtteranceDelay = TimeInterval.init(exactly: postUtteranceDelayInSecond)!
return utterance
}
let avSpeechSynthesizer = AVSpeechSynthesizer()
avSpeechSynthesizer.speak(getUtteranceWithDelay(preUtteranceDelayInSecond: 1, postUtteranceDelayInSecond: 2))
avSpeechSynthesizer.speak(getUtteranceWithDelay(preUtteranceDelayInSecond: 1, postUtteranceDelayInSecond: 2))
/*
**********************************
Voice identifier: com.apple.ttsbundle.Maged-compact
Voice language: ar-SA
Voice name: Maged
Voice quality: 1
**********************************
Voice identifier: com.apple.ttsbundle.Zuzana-compact
Voice language: cs-CZ
Voice name: Zuzana
import UIKit
// Step 1: Import AVFoundation library
import AVFoundation
class AVFoundataionDemoViewController: BaseViewController {
var avSpeechSynthesizer: AVSpeechSynthesizer!
@IBAction func startSpeechAction(_ sender: Any) {
// Step 4: Stop any existing speech, else it will not start !!!
avSpeechSynthesizer.stopSpeaking(at: .immediate)
func getUtterance(_ speechString: String) -> AVSpeechutterance {
let utterance = AVSpeechUtterance(string: speechString)
utterance.voice = ...
utterance.rate = ...
utterance.pitchMultiplier = ...
utterance.volume = ...
utterance.preUtteranceDelay = ...
utterance.postUtteranceDelay = ...
return utterance
}
/*
No compile and runtime error
Outcome: only "First AVSpeechSynthesizer" is outputed
*/
let firstAvSpeechSynthesizer = AVSpeechSynthesizer()
firstAvSpeechSynthesizer.speak(getUtterance("First AVSpeechSynthesizer"))
let secondAvSpeechSynthesizer = AVSpeechSynthesizer()
secondAvSpeechSynthesizer.speak(getUtterance("Second AVSpeechSynthesizer"))