3

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.

Wyetro
  • 8,439
  • 9
  • 46
  • 64
  • how about use a observer ? – KennyVB Oct 23 '16 at 06:20
  • @KennyVB, would that be able to work for an indefinite amount of time in either the background or while the phone is locked? – Wyetro Oct 24 '16 at 03:50
  • it will work until you stop observing... you can put a observer when it starts playing and stop it when it's finished så it dont break your app – KennyVB Oct 24 '16 at 09:23
  • I don't think that will work since that's pretty much what I am trying to do with `MPMusicPlayerControllerNowPlayingItemDidChange`. Also have you seen this http://stackoverflow.com/questions/4928207/receiving-notifications-with-app-in-background-mode? – Wyetro Oct 24 '16 at 17:50

1 Answers1

2

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.

shallowThought
  • 19,212
  • 9
  • 65
  • 112
  • Are you saying that when the app goes to the background (or device locks) to pause/stop the music? And to have it so that the app has to stay open until it the music finishes? And that there is no way to do it the way described in the question (which is ok if that's the case)? – Wyetro Oct 29 '16 at 20:14
  • Updated answer. – shallowThought Oct 30 '16 at 12:25
  • This doesn't help. My app needs to play a certain amount of songs and then stop when they're done. I want to be able to get notifications when the app is in the background so that it knows when to stop. – Wyetro Oct 31 '16 at 04:17
  • 1
    I understood. But as your approach is not possible, I was trying to point you to a another direction to work around. – shallowThought Oct 31 '16 at 11:17
  • It may have taken me a year to mark this as correct, but you were right - it was not possible in the end. – Wyetro Jul 20 '17 at 04:29