UIAlertController Managing system sounds in swift

279 Views Asked by At

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).

1

There are 1 best solutions below

0
On

As much as this is not an answer, it's not possible to accomplish this right now. As mentioned in this question/answer, it's against Apple's guidelines to use hardware buttons for extra behavior, but ironic since the camera app itself uses the volume button for the shutter.