This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class AVFoundataionDemoViewController: BaseViewController { | |
| let avSpeechSynthesizer = AVSpeechSynthesizer() | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| avSpeechSynthesizer.delegate = self | |
| } | |
| } | |
| extension AVFoundataionDemoViewController: AVSpeechSynthesizerDelegate { | |
| func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didStart utterance: AVSpeechUtterance) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var avSpeechSynthesizer: AVSpeechSynthesizer! | |
| override func viewWillDisappear(_ animated: Bool) { | |
| super.viewWillDisappear(animated) | |
| avSpeechSynthesizer.stopSpeaking(at: .immediate) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 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") | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| ********************************** | |
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func getUtterance(_ speechString: String) -> AVSpeechutterance { | |
| let utterance = AVSpeechUtterance(string: speechString) | |
| utterance.voice = ... | |
| utterance.rate = ... | |
| utterance.pitchMultiplier = ... | |
| utterance.volume = ... | |
| utterance.preUtteranceDelay = ... | |
| utterance.postUtteranceDelay = ... | |
| return utterance | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 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")) |