Home > Enterprise >  How to make a sound run every 10 seconds
How to make a sound run every 10 seconds

Time:09-06

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
       




    
    }

CodePudding user response:

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.

@IBAction func startBtn(_ sender: UIButton) {
    let timerForUpdateLabel = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateLabel), userInfo: nil, repeats: true)
    let timerForPlaySound = Timer.scheduledTimer(timeInterval: 10.0, target: self, selector: #selector(playSound), userInfo: nil, repeats: true)
};

@objc func updateLabel() {
    time5  = 1
    timer.text = String(time5)
}

@objc func playSound() {
    let url = Bundle.main.url(forResource: "mixkit-system-beep-buzzer-fail-2964", withExtension: "wav")
    player = try! AVAudioPlayer(contentsOf: url!)
    player.play()
}

Second one, if you just want to keep only one timer for simple

@IBAction func startBtn(_ sender: UIButton) {
    timer3 = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(action8888), userInfo: nil, repeats: true)
}; 

@objc func action8888() {
    time5  = 1
    timer.text = String(time5)
    if time5 % 10 == 0 // use mod here means everytime time5 divisible by 10 excute
    { 
        let url = Bundle.main.url(forResource: "mixkit-system-beep-buzzer-fail-2964", withExtension: "wav")
        player = try! AVAudioPlayer(contentsOf: url!)
        player.play()
        return
     }
}

CodePudding user response:

Refer the sample

import UIKit
import AVFoundation

class ViewController: UIViewController {
    
    var player: AVAudioPlayer!
    var timer: Timer!
    var time = 0
    var repeatInterval = 10
    
    override func viewDidLoad() {
        start()
    }
    
    func start() {
//        playSound()
        timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.playSound), userInfo: nil, repeats: true)
    }
    
    @objc func playSound() {
        time  = 1
        print(time)
        // update youre label here
        // timer.text = String(time5)
        if time % repeatInterval == 0 {
            let url = Bundle.main.url(forResource: "mixkit-system-beep-buzzer-fail-2964", withExtension: "wav")
            player = try! AVAudioPlayer(contentsOf: url!)
            player.play()
        }
    }
}
  • Related