我正在尝试使用 Swift 中的 AVFoundation Text to Speech 功能来说出根据参数更改的自定义字符串。如何实现单词之间的停顿?
假设这是我的字符串:
var spokenSentence = "I like Toast with lots of Butter, but banana is nice"
如何让 TTS 暂停,例如在“Butter”之后暂停 3 秒?
这是我关于 TTS 的代码:
var spokenSentence = "I like Toast with lots of Butter, but banana is nice"
let synth = AVSpeechSynthesizer()
var utterance = AVSpeechUtterance(string: spokenSentence)
及以后
synth.speak(utterance)
除了 MacOS 上的 swift 之外,我听说你可以使用 [[slnc 1000]] Swift 中有类似的函数吗?
最佳答案
似乎 AVSpeechUtterance
具有类似 preUtteranceDelay
的属性或postUtteranceDelay
。您可以通过编写一些预处理代码来利用此类功能:
extension AVSpeechSynthesizer {
func speekWithDelay(_ text: String) {
let pattern = #"([^{]*)(?:\{([0-9]+(?:\.[0-9]+))\})?"#
let regex = try! NSRegularExpression(pattern: pattern)
let matches = regex.matches(in: text, options: .anchored, range: NSRange(0..<text.utf16.count))
for match in matches {
let utteranceText = text[Range(match.range(at: 1), in: text)!]
let utterance = AVSpeechUtterance(string: String(utteranceText))
if let range = Range(match.range(at: 2), in: text) {
let delay = TimeInterval(text[range])!
utterance.postUtteranceDelay = delay
}
speak(utterance)
}
}
}
将其用作:
let synth = AVSpeechSynthesizer()
@IBAction func speakButtonPressed(_ sender: Any) {
let spokenSentence = "I like Toast with lots of Butter,{3} but banana is nice"
synth.speekWithDelay(spokenSentence)
}
请记住,AVSpeechSynthesizer
的实例需要保存在强引用中,直到说出最后一句话,因此您最好将其作为实例属性保存。
关于swift - 如何在 Swift 中用字符串进行文本到语音转换时产生 1-5 秒的小停顿?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55670011/