Not Receiving Remote Command Center events while using Music Player Controller

94 views Asked by At

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()
}
0

There are 0 answers