Search code examples
iosswiftswift3ios10mpmediaplayercontroller

MPMusicPlayerControllerNowPlayingItemDidChange in background


Is there a way to get the MPMusicPlayerControllerNowPlayingItemDidChange notification while the app is in the background?

I have an app that needs to be able to pause the music once a song has ended while in the background or when the screen is locked. I'm using a systemMusicPlayer to play the music.

I've tried adding the audio background capability and including a call to beginBackgroundTask in my applicationDidEnterBackground but that doesn't work for extended periods of time.


Solution

  • When in background, your app might be killed at any time, so you do not want to depend on acting in background.

    How I understood, what you want to achieve is that when your app goes to background, the currently playing music goes on but stops after the currently playing song finished.

    MPMusicPlayerController.systemMusicPlayer() is playing a queue of songs ("Playlist"). So I would try to manipulate this queue in applicationWillResignActive() to not have a song after the currently playing one .

    I did not test this and I am not sure whether or not this is possible through public API.