Last active
February 8, 2019 11:44
-
-
Save myrickchow32/073eb93907221e3a83d828e73121db3e to your computer and use it in GitHub Desktop.
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) | |
| avSpeechSynthesizer.speak(AVSpeechUtterance(string: "Hello world")) | |
| } | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| // Step 2: Init AVSpeechSynthesizer, which is the controller of what to speak and the status of speech. | |
| avSpeechSynthesizer = AVSpeechSynthesizer() | |
| // Step 3: Assign AVSpeechSynthesizerDelegate | |
| avSpeechSynthesizer.delegate = self | |
| } | |
| override func viewWillDisappear(_ animated: Bool) { | |
| super.viewWillDisappear(animated) | |
| print("viewWillDisappear") | |
| // Step 5: stop speaking to prevent blocking of deinit | |
| avSpeechSynthesizer.stopSpeaking(at: .immediate) | |
| // Step 5 (alternative): stop speaking to prevent blocking of deinit | |
| avSpeechSynthesizer.delegate = nil | |
| } | |
| override func viewDidDisappear(_ animated: Bool) { | |
| super.viewDidDisappear(animated) | |
| print("viewDidDisappear") | |
| } | |
| deinit { | |
| print("AVFoundationDemoViewController is deinit.") | |
| } | |
| } | |
| extension AVFoundataionDemoViewController: AVSpeechSynthesizerDelegate { | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment