Im using the code below to read random sentences at a random time. However I run into the problem when a random sentence is called to be read while the previous sentence is still being spoken by the AVSpeechSynthesizer, making the second sentence not spoken. What I am asking is, how can I get the second sentence to be spoken after the first sentence is finished being spoken??
Any import would be appreciated. Cheers
Heres my code:
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)
}
}