Hi All,
I’m trying to build a simple stopwatch app that I will use to time pool players when they play a 6 ball shootout (players break off and clear 6 balls in the quickest time).
The stopwatch part itself I have working fine with buttons for start, stop, reset and penalty (adds 5 seconds penalty to the time) and I have added the code to run a sound using AVAudioPlayer but the part I’m really stuck with is what code to put in to automatically play the sound when the timer reaches the same time of the first players time.
For example: player 1 breaks and clears all 6 balls in a time of 00:45:22 and when player 2 plays and their time reaches 00:45:22 the sound will play by itself without the need for manual button.
Currently I only know how to get Xcode to play a sound at a set time.
I would really appreciate any help with this as I’m very much a beginner with Xcode and a bit stumped after not finding anything specific to what I need online.
I’ve attached an image of my app so far. The top one is the running stopwatch and the player name to the left and under that I have the other player and also tried putting in a Text Field which I was trying to enter the first players time manually then reset the stopwatch and have the sound play when it reached the time I entered into the Text Field which hasn’t quite worked.
Here’s my code too.
Thanks.
//
// ViewController.swift
// 6 Ball Shootout Timer
//
// Created by Marcus Allen on 17/01/2023.
//
import UIKit
import AVFoundation
import SwiftUI
import ClockKit
class ViewController: UIViewController, UITextFieldDelegate{
var timer = Timer()
var (hours, minutes, seconds, fractions) = (0, 0, 0, 0)
var audioPlayer = AVAudioPlayer()
@IBOutlet weak var bg: UIImageView!
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var fractionsLabel: UILabel!
@IBOutlet weak var startOutlet: UIButton!
@IBOutlet weak var stopOutlet: UIButton!
@IBOutlet weak var resetOutlet: UIButton!
@IBOutlet weak var penaltyOutlet: UIButton!
@IBOutlet weak var timeToBeat: UITextField!
@IBAction func start(_ sender: UIButton) {
timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(ViewController.keepTimer), userInfo: nil, repeats: true)
startOutlet.isHidden = true
}
@IBAction func stop(_ sender: UIButton) {
timer.invalidate()
startOutlet.isHidden = false
}
@IBAction func reset(_ sender: UIButton) {
(hours, minutes, seconds, fractions) = (0, 0, 0, 0)
timerLabel.text = "00:00"
fractionsLabel.text = ":00"
}
@IBAction func penalty(_ sender: UIButton) {
seconds += 5
}
@IBAction func playerName(_ sender: UITextField) {
sender.resignFirstResponder()
}
override func viewDidLoad(){
super.viewDidLoad()
bg.loadGif(name: "vid2")
do
{let audioPath = Bundle.main.path(forResource: "endgame", ofType: ".mp3")
try audioPlayer = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath!))
}
catch {
//ERROR
} }
@objc func keepTimer() {
fractions += 1
if fractions > 99 {
seconds += 1
fractions = 0
}
if seconds == 60 {
minutes += 1
seconds = 0
}
let secondsString = seconds > 9 ? "\(seconds)" : "0\(seconds)"
let minutesString = minutes > 9 ? "\(minutes)" : "0\(minutes)"
timerLabel.text = "\(minutesString):\(secondsString)"
fractionsLabel.text = ":\(fractions)" }
}