I am creating a timer app, and I am trying to make it so that when the timer ends (seconds reaches 0), a system sound will keep playing until the user clicks a button on the device.
My preference for this button would be anywhere on the screen and the home/volume/ringer buttons e.t.c, similar to if you were silencing a phone ringtone when you get a call.
I currently have this code set up for a UIAlertController
let timerFinished = UIAlertController(title: "Timer Up", message: nil, preferredStyle: UIAlertControllerStyle.alert)
timerFinished.addAction(UIAlertAction(title: "OK", style: .cancel, handler: { (action: UIAlertAction!) in
}))
present(timerFinished, animated: true, completion: nil)
timerFinished.view.superview?.isUserInteractionEnabled = true timerFinished.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.alertClose)))
and
@objc func alertClose(gesture: UITapGestureRecognizer) {
self.dismiss(animated: true, completion: nil)
}.
So in essence, my questions are:
1) Is it possible to use physical buttons to run the actions of disabling the system sounds? - if so how?
2) How would you setup something to disable system sounds once an action is run? - would you set up a while loop that says something like:
while playSystemSound == true {
AudioServicesPlaySystemSound(SystemSoundID(THIS IS WHERE ID WOULD GO))
}
and run the code of:
playSystemSound = false
in the actions for the alertClose(), OK button and physical device buttons (if possible).