title: Use iPhone's headphones PlayPause button to execute code
body: <p>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).</p>
  
  <p>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).</p>
  
  <p>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.</p>
  
  <p>I do not manage to catch the button press, it just keeps playing/pausing music from my library.</p>
  
  <p>Currently the VC where I have the UIButton, this is the code I added, but I never enter the remoteControlReceived function:</p>
  
  <pre><code>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 ...
          }
      }
  }
  </code></pre>