【问题标题】:Swift text to speech快速文本到语音
【发布时间】:2017-06-20 18:53:49
【问题描述】:

我有一段代码不工作,但也没有给我运行时错误。扬声器没有说话。

let synth = AVSpeechSynthesizer()
var myUtterance = AVSpeechUtterance(string: audioTextField.text)
myUtterance.rate = 0.3
synth.speak(myUtterance)

有没有我遗漏的代码或者是其他什么?非常感谢您的帮助。

编辑:它在任何@IBActions 中都不起作用,但在加载功能视图中工作正常......

override func viewDidLoad() {
    super.viewDidLoad()
    speechRecognizer?.delegate = self
    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(tick), userInfo: nil, repeats: true)
    tick()
    requestSpeechAuth()
//WORKS HERE
}

@IBAction func audioButtonPressed(_ sender: Any) {
//DOESN"T WORK HERE    
    if isRecording {
        stopRecording()
    } else {
        startRecording()
    }
}

【问题讨论】:

    标签: swift text-to-speech


    【解决方案1】:

    此代码有效(来自 Apple 文档)

    let string = "Hello, World!"
    let utterance = AVSpeechUtterance(string: string)
    utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
    
    let synth = AVSpeechSynthesizer()
    synth.speak(utterance)
    

    记得导入 AVFoundation

    import AVFoundation
    

    【讨论】:

    • 是否支持ssml?
    • 我的意思是 ssml 到语音(而不是文本到语音)
    • 是否可以在某些地方暂停播放段落再次播放?
    • 当我的应用在后台 TTS 不工作时,是否可以让它工作?
    猜你喜欢
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 2023-03-03
    • 2021-12-17
    • 1970-01-01
    • 2011-06-08
    相关资源
    最近更新 更多