In my app, the user can say a command into his microphone when he presses a UIButton on screen (and it stops listening when he presses it again) and it then executes some code (-> this works).
I would like to do the same when the user presses his play/pause button on his headphones instead of the UIButton on screen (and that he is on this VC).
I've seen quite a few tutorials about how to use the iPhone headphones' play/pause button but I haven't been able to make them work for me.
I do not manage to catch the button press, it just keeps playing/pausing music from my library.
Currently the VC where I have the UIButton, this is the code I added, but I never enter the remoteControlReceived function:
override var canBecomeFirstResponder : Bool {
return true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.becomeFirstResponder()
UIApplication.shared.beginReceivingRemoteControlEvents()
}
override func remoteControlReceived(with event: UIEvent?) {
if (event?.type == UIEventType.remoteControl) {
if event?.subtype == UIEventSubtype.remoteControlTogglePlayPause {
//execute the code ...
}
}
}