swift - 如何在 Swift 中用字符串进行文本到语音转换时产生 1-5 秒的小停顿?

标签 swift avfoundation text-to-speech

我正在尝试使用 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/

相关文章:

java - 适用于 Android 的谷歌云语音 API

ios - 当 UIButton 的 subview 是 UITableViewCell 的 subview 或任何其他 View 的 subview 时,设置 UIButton 边缘插入的理想位置是什么

swift - addPeriodicTimeObserver 生成一个转义闭包错误

ios - 在播放视频组合时调整 AVSynchronizedLayer 中的 CALayers 大小

python - 如何使用文本转语音(pyTTS 或 SAPI5)延长单词之间的停顿

text-to-speech - 谷歌的文本到语音引擎的声音?

ios - 带有 UIAlertAction swift 的增量标签栏角标(Badge)?

function - Swift func - 不符合协议(protocol) "Boolean Type"

avfoundation - 使 CIContext.render(CIImage, CVPixelBuffer) 与 AVAssetWriter 一起工作

ios - 使用 AVFoundation 在时域中记录位置