I tried everything above, but, unfortunately, now nothing seems to work. Then I looked at beginReceivingRemoteControlEvents
and found that it
On iOS 7.1 and later, use the MPRemoteCommandCenter shared object to record remote control events. You do not need to call this method when using the common object of the command center.
Then I checked MPRemoteCommandCenter
and finally ended up on the MPRemoteCommand
documentation MPRemoteCommand
.
Itβs good that there is such an example:
let commandCenter = MPRemoteCommandCenter.shared() commandCenter.playCommand.addTarget(handler: { (event) in // Begin playing the current track self.myMusicPlayer.play() return MPRemoteCommandHandlerStatus.success })
Now, if we want to read the middle button, we can do:
MPRemoteCommandCenter.shared().togglePlayPauseCommand.addTarget { (event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus in // middle button (toggle/pause) is clicked print("event:", event.command) return .success }
This works, and I managed to detect the middle button of the headphones.
Note: I noticed that there is another behavior that depends on where we placed such code above. That is, when I put the View Controller, the reported events are identical, and when I put it in AppDelegate, the didFinishLaunching of the reported events are different. In any case, the event is detected.
Fittsu source share