1

I am new to coding. I am trying to learn iOS development and I have created a simple countdown timer, that uses a slider to select the amount of time in seconds. I would like to add a tone to the final 9 secs of the countdown. I know how to add audio files, but I have "NO IDEA" how to add a countdown tone to the last 9 seconds of the timer.

@IBAction func startBtnTapped(_ sender: Any) {
    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.counter), userInfo: nil, repeats: true)

    SoundPlayer4.play()

    //Hide Start and Slider Buttons
    startBtn.isHidden = true
    sliderCtrl.isHidden = true
    stopBtn.isHidden = false

}

@IBAction func stopBtnTapped(_ sender: Any) {
    timer.invalidate()
    seconds = 180
    sliderCtrl.setValue(180, animated: true)
    timerLbl.text = "180"

    SoundPlayer3.play()

    //Show Start and Slider Buttons
    sliderCtrl.isHidden = false
    startBtn.isHidden = false
    stopBtn.isHidden = true
}

@IBAction func sliderCrtlUsed(_ sender: UISlider) {
    seconds = Int(sender.value)
    timerLbl.text = String(seconds)
}

func counter(){
    seconds -= 1
    timerLbl.text = String(seconds)

    if (seconds == 0){
        timer.invalidate()

        //Show Start and Slider Buttons
        startBtn.isHidden = false
        sliderCtrl.isHidden = false
        stopBtn.isHidden = true

        //Play Audio
        SoundPlayer1.play()
    }
}

So can anyone help me add a countdown tone to my project? Thank you

blaq
  • 59
  • 1
  • 8

1 Answers1

0

Put this code in counter function:

if seconds < 10 {

    var AudioURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Dog bark", ofType: "mp3")!)
    var AudioPlayer = AVAudioPlayer()

    do {       
        AudioPlayer = try AVAudioPlayer(contentsOfURL: AudioURL)
    } catch _ as NSError {
        fatalError()
    }

    AudioPlayer.play()
}

Then put the file here:

enter image description here

And that's it!

You can read more on this question here.

Community
  • 1
  • 1
Bright
  • 5,699
  • 2
  • 50
  • 72