Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AVSpeechSynthesizer在添加新字符串以进行语音时不工作[xcode-swift 4]_Swift_Xcode_Text To Speech_Speech_Avspeechsynthesizer - Fatal编程技术网

AVSpeechSynthesizer在添加新字符串以进行语音时不工作[xcode-swift 4]

AVSpeechSynthesizer在添加新字符串以进行语音时不工作[xcode-swift 4],swift,xcode,text-to-speech,speech,avspeechsynthesizer,Swift,Xcode,Text To Speech,Speech,Avspeechsynthesizer,我用下面的代码在随机时间读随机的句子。 然而,我遇到了一个问题,当一个随机的句子被调用来阅读,而前一个句子仍然由AVSpeechSynthesizer说出,使得第二个句子不被说出。我想问的是,在第一句话说完后,我如何才能让第二句话被说出来 任何进口商品都将不胜感激。 干杯 这是我的密码: import UIKit import AVFoundation class ViewController: UIViewController { var myTimer = Timer() let

我用下面的代码在随机时间读随机的句子。 然而,我遇到了一个问题,当一个随机的句子被调用来阅读,而前一个句子仍然由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)
}


}   

您只需使用
avspeechsynthezerdelegate
,就可以从代码中删除计时器

要使用
avspeechsynthezerdelegate
首先需要使用
avspeechsynthezerdelegate
确认视图控制器,如下所示:

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=”“