Swift 文字转语音
Swift text to speech
我有一段代码无法运行,但也没有出现运行时错误。演讲者没有讲话。
let synth = AVSpeechSynthesizer()
var myUtterance = AVSpeechUtterance(string: audioTextField.text)
myUtterance.rate = 0.3
synth.speak(myUtterance)
是否有任何我遗漏的代码或其他代码?将不胜感激。
编辑:它在任何@IBActions 中都不起作用,但在加载函数的视图中工作正常....
override func viewDidLoad() {
super.viewDidLoad()
speechRecognizer?.delegate = self
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(tick), userInfo: nil, repeats: true)
tick()
requestSpeechAuth()
//WORKS HERE
}
@IBAction func audioButtonPressed(_ sender: Any) {
//DOESN"T WORK HERE
if isRecording {
stopRecording()
} else {
startRecording()
}
}
此代码有效(来自 Apple 文档)
let string = "Hello, World!"
let utterance = AVSpeechUtterance(string: string)
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
let synth = AVSpeechSynthesizer()
synth.speak(utterance)
记得导入 AVFoundation
import AVFoundation
我有一段代码无法运行,但也没有出现运行时错误。演讲者没有讲话。
let synth = AVSpeechSynthesizer()
var myUtterance = AVSpeechUtterance(string: audioTextField.text)
myUtterance.rate = 0.3
synth.speak(myUtterance)
是否有任何我遗漏的代码或其他代码?将不胜感激。
编辑:它在任何@IBActions 中都不起作用,但在加载函数的视图中工作正常....
override func viewDidLoad() {
super.viewDidLoad()
speechRecognizer?.delegate = self
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(tick), userInfo: nil, repeats: true)
tick()
requestSpeechAuth()
//WORKS HERE
}
@IBAction func audioButtonPressed(_ sender: Any) {
//DOESN"T WORK HERE
if isRecording {
stopRecording()
} else {
startRecording()
}
}
此代码有效(来自 Apple 文档)
let string = "Hello, World!"
let utterance = AVSpeechUtterance(string: string)
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
let synth = AVSpeechSynthesizer()
synth.speak(utterance)
记得导入 AVFoundation
import AVFoundation