macOS:Mojave 10.14.4 测试版
iOS:12.2 测试版
Xcode:10.2 测试版
我正在使用
AVSpeechSynthesizer
但下面的代码不会从暂停的地方恢复。
// The pause functionality works fine
if (synth.isSpeaking) {
synth.pauseSpeaking(at: AVSpeechBoundary.word)
}
// But continueSpeaking always starting from the beginning.
if (synth.isPaused) {
synth.continueSpeaking();
}
我如何从离开的地方继续?我还缺少什么吗?
我实现了以下代码来检查您的问题(在
Mojave 10.14.4
,iOS 12.2
,Xcode 10.2.1
和swift 5.0
下进行测试):
class SpeechSynthesis: UIViewController {
var synthesizer = AVSpeechSynthesizer()
var playQueue = [AVSpeechUtterance]()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
for i in 1...10 {
let stringNb = "number " + String(i) + " of the speech synthesizer."
let utterance = AVSpeechUtterance(string: stringNb)
playQueue.append(utterance)
}
for utterance in playQueue {
synthesizer.speak(utterance)
}
}
@IBAction func pauseButton(_ sender: UIButton) {
if (synthesizer.isSpeaking == true) {
if (synthesizer.pauseSpeaking(at: .immediate) == true) {
print("PAUSE")
} else {
print("P.R.O.B.L.E.M. when pausing.")
}
}
}
@IBAction func resumeButton(_ sender: UIButton) {
if (synthesizer.isPaused == true) {
if (synthesizer.continueSpeaking() == true) {
print("CONTINUE")
} else {
print("P.R.O.B.L.E.M. when resuming.")
}
}
}
}
我注意到边界的另一个问题
.word
在触发时并不总是暂停,但是当这个约束更改为.immediate
时,一切都会从暂停的地方恢复。
但是,当它很少在边界
.word
处暂停时,它也总是从暂停的地方恢复。
我不知道你的问题来自哪里,但是通过上面提到的配置和这个代码片段,语音合成器从暂停的地方恢复。