我已经在我的应用程序中实现了文本到语音转换,它与我当前使用的代码配合得很好。基本上,算法会创建一个文本,然后如果用户单击 UIButton,就会朗读该文本。
挑战:我想启用相同的 UIButton 来暂停合成器,如果该按钮已经被点击(即当前正在朗读文本),然后在它停止的地方继续朗读,如果再次点击按钮。
我知道 AVFoundation Reference 中有一些函数,但我无法正确实现它们。
有谁知道如何在 Swift 中执行此操作?
import UIKit
import AVFoundation
@IBOutlet var generatedText: UILabel!
@IBAction func buttonSpeakClicked(sender: UIButton){
var mySpeechSynthesizer:AVSpeechSynthesizer = AVSpeechSynthesizer()
var mySpeechUtterance:AVSpeechUtterance = AVSpeechUtterance(string:generatedText.text)
mySpeechUtterance.rate = 0.075
mySpeechSynthesizer .speakUtterance(mySpeechUtterance)
}
最佳答案
AVSpeechSynthesizer Class Reference
这些方法你试过吗?
- pauseSpeakingAtBoundary:
和 - continueSpeaking
还有一些属性(paused
和 speaking
)可以帮助您确定合成器的状态。
这样的代码应该可以工作:mySpeechSynthesizer.pauseSpeakingAtBoundary(AVSpeechBoundary.Immediate)
关于swift - 如何快速暂停和恢复 AVSpeechSynthesizer/AVSpeechUtterance?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26038749/