Swift|17 做出告白機器人
Published in
Aug 14, 2021
今天是七夕情人節
用可愛的popcat來做出告白機器人~
學習使用 AVSpeechSynthesizer 講話
第一 先用popcat印出歌詞
第二 用popcat來說話
import UIKitimport AVFoundationclass ViewController: UIViewController {@IBOutlet weak var loveTextField: UITextField!override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view.}@IBAction func speak(_ sender: Any) {let speechUtterance = AVSpeechUtterance(string: loveTextField.text!)speechUtterance.voice = AVSpeechSynthesisVoice(language: "zh-TW")speechUtterance.rate = 0.55 //加快語速let synthesizer = AVSpeechSynthesizer()synthesizer.speak(speechUtterance)}}
希望下次可以更新成按了popcat嘴巴會動的版本