swift - AVSpeechSynthesizer 在添加要说出的新字符串时不起作用 [xcode - swift 4]

标签 swift xcode text-to-speech speech avspeechsynthesizer

我使用下面的代码在随机时间读取随机句子。 但是,当 AVSpeechSynthesizer 仍在说前一个句子时调用随机句子进行阅读时,我遇到了问题,导致第二个句子没有说出来。我想问的是,如何让第一句说完后说出第二句??

任何进口将不胜感激。 干杯

这是我的代码:

import UIKit
import AVFoundation


class ViewController: UIViewController {

var myTimer = Timer()
   let string = ["what kind of car do you have?", "do you like the beach?","did you bring a towel?","There are big waves today"]
var randomTimer = Int()


@objc func speakToMe(){

    let random = Int.random(in: 0...3)
    randomTimer = Int.random(in: 0...2)
    print(randomTimer)
    print(string[random])


let utterance = AVSpeechUtterance(string: string[random])
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
utterance.rate = 0.1
let synthesizer = AVSpeechSynthesizer()
synthesizer.speak(utterance)

}

override func viewDidLoad() {
    super.viewDidLoad()
    speakToMe()
    myTimer = Timer.scheduledTimer(timeInterval: TimeInterval(randomTimer), target: self, selector: #selector(ViewController.speakToMe), userInfo: nil, repeats: true)
}


}   

最佳答案

为此,您可以简单地使用 AVSpeechSynthesizerDelegate,并且可以从代码中删除计时器。

要使用 AVSpeechSynthesizerDelegate,您首先需要使用 AVSpeechSynthesizerDelegate 确认您的 View Controller ,如下所示:

class ViewController: UIViewController, AVSpeechSynthesizerDelegate {

接下来你需要添加

synthesizer.delegate = self

在您的 viewDidLoad 方法中。你需要声明

let synthesizer = AVSpeechSynthesizer()

在方法之外和类之内。

并且您可以使用 randomElement 属性从 string 数组中找到随机元素。

您的最终代码将如下所示:

import UIKit
import AVFoundation

class ViewController: UIViewController, AVSpeechSynthesizerDelegate {

    let string = ["what kind of car do you have?", "do you like the beach?","did you bring a towel?","There are big waves today"]
    let synthesizer = AVSpeechSynthesizer()

    override func viewDidLoad() {
        super.viewDidLoad()
        synthesizer.delegate = self
        speakToMe()
    }

    @objc func speakToMe(){

        let utterance = AVSpeechUtterance(string: string.randomElement()!)
        utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
        utterance.rate = 0.1
        synthesizer.speak(utterance)

    }

    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
        speakToMe()
    }
}

编辑:

由于您在数组中只使用了 4 个元素,因此当您为其取随机字符串时,可能会多次重复相同的句子,因此您可以在此处添加一个逻辑来防止它发生。

像下面这样更新你的speakToMe函数:

@objc func speakToMe(){

    var randomStr = string.randomElement()!
    while previousStr == randomStr {
        randomStr = string.randomElement()!
    }
    previousStr = randomStr
    let utterance = AVSpeechUtterance(string: randomStr)
    utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
    utterance.rate = 0.1
    synthesizer.speak(utterance)
}

并在函数外声明var previousStr = ""

关于swift - AVSpeechSynthesizer 在添加要说出的新字符串时不起作用 [xcode - swift 4],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55254673/

相关文章:

swift - Firebase UpdateChildValues 没有设置任何值

ios - MKMapView 告诉 map 用填充显示 MKAnnotations?

swift - 将 [Float] 转换为变量参数

excel - 我可以在 Excel 宏中模拟按 Enter 键吗?

python - Azure 文本转语音并使用 python 在虚拟麦克风中播放

swift - 为什么 "Verify your bundle identifier ' org.cocoapods.Alamofire' 是正确的。”?

iphone - View 加载时播放声音

ios - Cocos2d - 更改为背景图像

IOS 项目在 xcode 7.1 上显示错误 "An internal error occurred. Editing functionality may be limited"

android - 自动下载android TTS引擎