swift - 如何快速暂停和恢复 AVSpeechSynthesizer/AVSpeechUtterance?

标签 swift avfoundation text-to-speech avspeechsynthesizer

我已经在我的应用程序中实现了文本到语音转换,它与我当前使用的代码配合得很好。基本上,算法会创建一个文本,然后如果用户单击 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

还有一些属性(pausedspeaking)可以帮助您确定合成器的状态。

这样的代码应该可以工作:mySpeechSynthesizer.pauseSpeakingAtBoundary(AVSpeechBoundary.Immediate)

关于swift - 如何快速暂停和恢复 AVSpeechSynthesizer/AVSpeechUtterance?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26038749/

相关文章:

python - 如何在 python 中将文本转换为语音(mp3 文件)?

ios - 谷歌地图缩小到 GPS 和标记

swift - 将原型(prototype)单元连接到 View Controller [Swift 4]

ios - 错误 : Initializer for optional binding must have Optional type, 不是 'DetailViewController'

swift - 检查对象是否是特定类并符合协议(protocol)

ios - AudioKit:如何点击同一输入进行多次使用(AudioKitUI + 语音识别)

swift - 使用 CVMetalTextureCacheCreateTextureFromImage 在 Swift 中将 CMSampleBuffer 转换为 CVMetalTexture

ios - 如何在 iOS 中检查视频的分辨率和比特率

android - 如何使用话语 ID 实现句子的流畅文本到语音转换

audio - 用于脚本WAV或MP3输出的文本到语音(TTS)软件