Here is the code I am using this is a timer and I need the sound to play every ten seconds how can I do this?
@IBAction func startBtn(_ sender: UIButton) {
timer3 = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.action8888), userInfo: nil, repeats: true)
}; @objc func action8888() {
time5 += 1
timer.text = String(time5)
if time5 == 10.0 {
let url = Bundle.main.url(forResource: "mixkit-system-beep-buzzer-fail-2964", withExtension: "wav")
player = try! AVAudioPlayer(contentsOf: url!)
player.play()
return
}
2
Answers
Your code seems wrong because you increase time5 every seconds and update time label. But only when time5 == 10.0 you play. So you just only play for only one time at 10.0s
There are two approach for you to do this
First one, have a timer which every 10.0s play sound and one every 1.0s to update time.
Second one, if you just want to keep only one timer for simple
Refer the sample