3

I am using MPRemoteCommandCenter and MPNowPlayingInfoCenter to control the playback of audio in the background. To setup command callbacks:

let commandCenter = MPRemoteCommandCenter.sharedCommandCenter()

commandCenter.pauseCommand.enabled = true
commandCenter.pauseCommand.addTarget(self, action: #selector(AudioPlayer.remoteCmdPause))

commandCenter.playCommand.enabled = true
commandCenter.playCommand.addTarget(self, action: #selector(AudioPlayer.remoteCmdplay))

And to update audio data:

let artWork = MPMediaItemArtwork(image: image)
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = [
   MPMediaItemPropertyTitle: self.title,
   MPMediaItemPropertyArtwork: artWork,
   MPMediaItemPropertyPlaybackDuration: self.duration,
   //MPNowPlayingInfoPropertyElapsedPlaybackTime: 0
]

This shows the pause/play button which I can handle in the remoteCmdPause and remoteCmdPlay callbacks. It also shows the volume bar which I want to either hide or register a handler so I can react to volume changes.

Is it possible to register a call back for volume changes in order to perform actions other than (or besides) increasing/decreasing the volume?
If not is it possible to hide the volume control bar from the lock screen when playing back audio in the background?

sigrem
  • 57
  • 1
  • 5
  • Hi, could you tell me where exactly to put the lines you have shown above? Do i have to call those lines AFTER audio playback has began? I am unable to get any lock screen controls to display, thanks for your help – MikeG Mar 07 '17 at 18:50
  • I have a singleton to handle the background state. In its `init()` I setup the AVAudioSession. At some point later I call a configure function on the singleton to enable commands and setup callbacks (the first set of lines above). The initial values for `nowPlayingInfo` is also set during configure (the second set of line). `nowPlayingInfo` can be updated on a regular basis afterwards if needed. I.e. the audio session is set and call backs are configured before audio playback starts. – sigrem Mar 08 '17 at 19:47
  • So when the app falls into the background, the `init()` is called at that point? But haven't you already setup an AVAudioSession before playing audio while the app was in the active state? – MikeG Mar 08 '17 at 21:01
  • The `init()` must be called before the app falls into the background. In my case I create the singleton in one of my view controllers but I believe you can create it from the app delegate to make sure audio session is setup in advance and commands are configured before the app goes into the background. – sigrem Mar 09 '17 at 15:52
  • Ok I see but for some reason I'm still struggling with this, if you have a second could you look at [my question here](http://stackoverflow.com/questions/42656940/audio-playback-lock-screen-controls-not-displaying-on-iphone) , I just can't figure out why this wont work. Thanks – MikeG Mar 09 '17 at 16:19
  • I don't have enough reputation points to add comments to your other question so I add it here: Yes, for the lock screen to work you need to use iOS APIs to play audio. Not sure how Spotify does it but they may be using a second audio session in parallel for this purpose and use the controls to control both. Your background handler (the singleton in my case) could start playing the second audio with 0 volume when it goes into background and stop it when in foreground. I haven't tested it myself but an option to try. – sigrem Mar 09 '17 at 17:47
  • Ah ok I see. Thanks for the help i really appreciate it! – MikeG Mar 09 '17 at 21:28
  • Added my answer to your question in the other thread. – sigrem Mar 13 '17 at 14:13

1 Answers1

0

As for the callback part of my question, there does not seem to be any specific remote command to handle the volume changes from the lock screen. Just observing the "outputVolume" (via observeValueForKeyPath) does the trick.
Hiding the volume bar is still a question but not necessary anymore (at least for my part).

sigrem
  • 57
  • 1
  • 5