1

I am trying to receive notifications from the remote command center for when the play/pause button is tapped so that I can appropriately update the image for my play/pause button. However, I am not receiving the notification of when the playcommand/pausecommand is pressed (ie. when it should print "tapped play" or "tapped pause"). This is my first time using this library, and so I followed Apples docs and it says to implement a music controller using either a applicationMusicPlayer or applicationQueuePlayer to receive these events, but so far I am not able to. I don't know if there is anything else I need to do besides just setting it up as a applicationQueuePlayer.

Here is my code for a very plain music player controller that produces this situation:

let musicPlayer = MPMusicPlayerController.applicationQueuePlayer

override func viewDidLoad() {
   super.viewDidLoad()
   setupRemoteControl()
   musicPlayer.setQueue(with: .songs())
}

func setupRemoteControl() {
   UIApplication.shared.beginReceivingRemoteControlEvents()
        
   let commandCenter = MPRemoteCommandCenter.shared()
        
   commandCenter.playCommand.isEnabled = true
   commandCenter.playCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
      print("tapped play")
      return .success
   }
   
   commandCenter.pauseCommand.isEnabled = true
   commandCenter.pauseCommand.addTarget {(_) -> MPRemoteCommandHandlerStatus in
      print("tapped pause")
      return .success
}

@IBAction func selectSongs(_ sender: UIButton) {
   let controller = MPMediaPickerController(mediaTypes: .music)
   controller.allowsPickingMultipleItems = true
   controller.popoverPresentationController?.sourceView = sender
   controller.delegate = self
   present(controller, animated: true, completion: nil)
}
    
    
func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
   musicPlayer.setQueue(with: mediaItemCollection)
   mediaPicker.dismiss(animated: true, completion: nil)
   musicPlayer.play()
}
WY34
  • 261
  • 3
  • 10
  • I can confirm these commands are no longer called for any of the MPMusicPlayerControllers presumably as they seems to be acting as their own now playing apps. The closest you can come is to register for MPMusicPlayerControllerNowPlayingItemDidChangeNotification & MPMusicPlayerControllerPlaybackStateDidChangeNotification. Note those arent perfect either and very difficult to detect when a user has skipped to next/previous or beginning/end. – RunLoop Feb 28 '21 at 08:10

0 Answers0