'Building an iOS shot clock App with multiple timers and sounds
I have made a countdown timer which I will use for pool tournaments as a shot clock and I have managed to add a sound for when the timer reaches 5 seconds left but I wanted another sound to be played at 5 seconds down to 1 second and also a separate sound at 0 seconds which I’ve dragged over to my sidebar and I have working functions for start/stop/extension etc.
I tried using one single audio file that I stitched together to run from 5 seconds to make it easier but I found a problem that when the shot clock reaches 5 and it starts to play that file and if I push the stop/restart button then it will simply pause the audio file and then when the next shot clock countdown reaches 5 seconds the file begins from the point that it was stopped "paused" so just wanted to ask if there was a line of code that I can add to my current code to reset the stitched single audio file that I have or play a single sound for each second 5 to 0?
for reference, I'm a beginner at using Xcode :)
Here is where I am currently.
//
// ViewController.swift
// Shot Clock 5
//
// Created by Marcus Allen on 16/03/2022.
//
import UIKit
import AVFoundation
import SwiftUI
class ViewController: UIViewController, UITextFieldDelegate{
var seconds = 30
var timer = Timer()
var audioPlayer = AVAudioPlayer()
var count = 0
@IBOutlet weak var label: UILabel!
@IBOutlet weak var sliderOutlet: UISlider!
@IBAction func slider(_ sender: UISlider)
{
seconds = Int(sender.value)
label.text = String(seconds)
}
@IBAction func name2(_ sender: UITextField) {
sender.resignFirstResponder()
}
@IBAction func name1(_ sender: UITextField) {
sender.resignFirstResponder()
}
@IBAction func score2(_ sender: UITextField) {
sender.resignFirstResponder() }
@IBAction func score1(_ sender: UITextField) {
sender.resignFirstResponder()
}
@IBOutlet weak var startOutlet: UIButton!
@IBAction func start(_ sender: Any)
{
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.counter), userInfo: nil, repeats: true)
sliderOutlet.isHidden = true
startOutlet.isHidden = true
}
@objc func counter()
{
seconds -= 1
label.text = String(seconds) + ""
if (seconds == 0)
{
timer.invalidate()
}
if (seconds == 5)
{
do {
}
sliderOutlet.isHidden = false
startOutlet.isHidden = false
audioPlayer.play()
}
}
@IBAction func EXT2(_ sender: UIButton) {seconds += 15 }
@IBAction func ext2pressed(_ sender: UIButton) {
sender.backgroundColor = sender.backgroundColor == UIColor.red ? UIColor.black : UIColor.red }
@IBAction func EXT1(_ sender: UIButton) {seconds += 15 }
@IBAction func ext1pressed(_ sender: UIButton) {
sender.backgroundColor = sender.backgroundColor == UIColor.red ? UIColor.black : UIColor.red
}
@IBOutlet weak var stopOutlet: UIButton!
@IBAction func stop(_ sender: Any)
{
timer.invalidate()
seconds = 30
sliderOutlet.setValue(30 , animated: true)
label.text = "30"
audioPlayer.stop()
sliderOutlet.isHidden = false
startOutlet.isHidden = false
}
override func viewDidLoad()
{
super.viewDidLoad()
do
{
let audioPath = Bundle.main.path(forResource: "3", ofType: ".mp3")
try audioPlayer = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath!))
}
catch {
//ERROR
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
