7 Replies
      Latest reply on Mar 11, 2018 10:09 AM by LunarBase
      jeffb6688 Level 1 Level 1 (0 points)

        I need to display iPod (transport) controls and receive events to control the MPMoviePlayerController player in the background. I've implemented the following:

         

                - (void)remoteControlReceivedWithEvent:(UIEvent *)theEvent {

                NSLog("Not getting here");

         

            }

         

            - (void)viewDidAppear:(BOOL)animated {

                [super viewDidAppear:animated];

                [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

                [self becomeFirstResponder];

            }

         

            - (BOOL)canBecomeFirstResponder {

                return YES;

            }

         

        I've also activated the audio session and

         

              if (![session setCategory:AVAudioSessionCategoryPlayback

                         withOptions:AVAudioSessionCategoryOptionMixWithOthers

                               error:&setCategoryError]) {

              // handle error

            }

         

         

        Finally, my info.plist is set up to enable the application to run in the background.

         

        If I am playing media through the movie player and I background the app, I don't even get a display of the controls. I've tried implementing this in both a singleton and a view controller.

         

        What's more mysterious is that if I am playing a song through the MPMusicPlayerController player when I background the app, I do see controls and the controls actually work. But no events from those controls are received by my method remoteControlReceivedWithEvent.

         

        What do I need to do to see the controls and to receive events from the controls?

        • Re: Unable To Receive remoteControlReceivedWithEvent: (UIEvent*) event
          gabejacobs Level 1 Level 1 (0 points)

          Wondering if you found any solution to this?

            • Re: Unable To Receive remoteControlReceivedWithEvent: (UIEvent*) event
              theanalogkid Apple Staff Apple Staff (610 points)

              If I recall you need to be the "Now Playing" app to receive any remote events, and you cannot be "Now Playing" when you are mixable.

                • Re: Unable To Receive remoteControlReceivedWithEvent: (UIEvent*) event
                  Spudfish Level 1 Level 1 (0 points)

                  I am having a similar problem, but trying to use the MPRemoteCommandCenter as described in the documentation:

                       Using Remote-Control Events with a Music Player

                       Users can initiate audio playback commands through an external headset or accessory. These commands are sent to the app as remote-control events. To           receive remote-control events in your app, you must use an application music player.

                   

                  I am using the following skeleton code to set up the Application Music Player in my main view controller:

                          NSLog("Setting audio session")
                          do {
                              try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
                              try AVAudioSession.sharedInstance().setActive(true)
                          } catch {
                              NSLog("\(error)")
                          }
                          NSLog("Instantiating the player")
                          applePlayer = MPMusicPlayerController.applicationMusicPlayer()
                  
                  
                  

                   

                  Then this to set up the remote command targets, just for the play/pause button to try this out:

                          NSLog("Registering remote events")
                          let rcc = MPRemoteCommandCenter.sharedCommandCenter()
                          let commands = [rcc.playCommand, rcc.pauseCommand, rcc.togglePlayPauseCommand]
                          commands.forEach({ theCommand in
                              theCommand.enabled = true
                              theCommand.addTarget(self, action: #selector(SSViewController.playOrPauseMusic(_:))
                          })
                  
                  
                  

                   

                  "playOrPauseMusic" is the same handler that the UI uses to toggle the play/pause state:

                      @IBAction func playOrPauseMusic(sender: AnyObject) {
                          if applePlayer.playbackState == .Playing {
                              applePlayer.pause()
                          } else {
                              applePlayer.play()
                          }
                      }
                  
                  
                  

                   

                  and I start playing an album given its persistent ID via:

                              let theQuery = MPMediaQuery(filterPredicates: [MPMediaPropertyPredicate(value: theAlbumID, forProperty: MPMediaItemPropertyAlbumPersistentID)])
                              applePlayer.setQueueWithQuery(theQuery)
                              applePlayer.play()
                  
                  
                  

                   

                  The music starts playing as expected, and the UI works to play or pause the music. Since I have set the required background mode to "audio", the application continues playing music when placed in the background. Whether I am in the application and attempting to control the music with headphone buttons, or outside the app from the control center or lock screen, the remote commands are never getting to my "playOrPauseMusic" handler. Likely related, setting the NowPlayingInfoCenter information with custom information from my application does not work. It seems like the applicationMusicPlayer object is behaving like its own "Now Playing App" and not allowing my application to make use of this technology, despite the above description to the contrary.

                  Am I missing anything? I have also tried to use the mechanism that the original poster tries (remoteControlReceivedWithEvent), but that doesn't work either, even when I am not "mixable".

                  • Re: Unable To Receive remoteControlReceivedWithEvent: (UIEvent*) event
                    gabejacobs Level 1 Level 1 (0 points)

                    Can you explain this? I'm able to recieve these events with AVPlayer, but not playing from MPMusicPlayerController using Apple Music ids. Background notifications also do not fire.

                  • Re: Unable To Receive remoteControlReceivedWithEvent: (UIEvent*) event
                    jeffb6688 Level 1 Level 1 (0 points)

                    No, I still have not found a solution. Sorry for the delayed response. Have you since found a solution? The comments that followed your comments are not helpful either.

                  • Re: Unable To Receive remoteControlReceivedWithEvent: (UIEvent*) event
                    jerlandson2 Level 1 Level 1 (0 points)

                    Any solutions?  I am facing this same issue....