13

I have an app, that will keep track of everything the user do in the iPod app. To do this, I added few observers to NSNotificationCenter, like MPMusicPlayerControllerNowPlayingItemDidChangeNotification. But my problem is, I only get those notifications when my app is in the foreground, if its in the background, the system add the notification to a queue, and then the next time my app becomes active it delivers it to me. I have no interest in this queue, since I want to receive real-time notifications.

Is there any way for me to get those notifications even if my app is in suspended state? I want to run just 3 lines of code everytime I get this NowPlayingItemDidChange notifications for example.

Here is where I add the observer.

MPMusicPlayerController *iPodMediaPlayer = [MPMusicPlayerController iPodMusicPlayer];



NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

     [notificationCenter addObserver: self selector: @selector(handle_NowPlayingItemChanged:) name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification

                                    object:iPodMediaPlayer];


 [iPodMediaPlayer beginGeneratingPlaybackNotifications];

Also, if I add another kind of object to the observer instead of iPodMediaPlayer, the observer won't call the method.

Thanks a lot,

Abras

Abras
  • 361
  • 1
  • 4
  • 14

3 Answers3

6

iOS applications are suspended when they are not in the foreground. There are three exceptions to this rule. You can have code execute in the background if your application is

a) Playing audio. This means the application itself is actually generating audio. My understanding is that the MPMediaPlayerController iPodMusicPlayer object only controls the playback of the external iPod process, rather than playing audio from the app itself. Perhaps you could have some success if you called applicationMusicPlayer instead of iPodMusicPlayer and set the appropriate background flags in your applications Info.plist. This seems like the most legitimate way to get your application to work, but you wouldn't be able to control iPod playback from the iPod app, only your app and the system audio controls.

b) Get your app to monitor the location. If the app is using the GPS it can continue to execute in the background. Downside to this is that the GPS will drain battery, and users might be creeped out that you're requesting their location.

c) Ask UIApplication for extra time. If you use UIApplication's beginBackgroundTask method, your application will continue to run for a finite amount of time in the background. If your users are going to come into your application once every ten minutes or so, this could work as well.

Hope that helps.

  • 1
    Yes, I know there is only this three options. But I'd thought maybe there is a way to register the notification in the system, then your app would enter background mode normally. If the user do something that triggers the notification, then the Notification Center would wake your app just so it can respond to the notification. After responding, it would return to background mode as normal. – Abras Feb 08 '11 at 18:29
  • @Abras: Did you find out how to do this? I have an audio app. And I want it to receive notifications(when other audio apps come up) when it is in the background. How do I do it? – Namratha Aug 04 '11 at 13:33
  • @Abras Did you ever figure this out? – Wyetro Oct 31 '16 at 04:14
0

As I explained here iOS receive Media Player Notifications when app is in background, there seems no way to get notifications from iPodMusicPlayer.

About Omar Raul Qazi answer:

a) i tried and I had no success. The music kept going down when pressing home button. I think this background flag only works for normal AudioSessions and not for MPMusicPlayer...

b) I am not sure this would work and I don't think Apple would like it when looking for approval

c) You can run in background only synchronous task. You cannot wait there for a notification. Am I wrong?

Community
  • 1
  • 1
super
  • 411
  • 3
  • 16
0

Multitasking in iOS is currently a very restricted one. You may start a background task using beginBackgroundTaskWithExpirationHandler: method (of an UIApplication object), but it is intended to finish a finite-length task before going suspended. All background tasks may expire (and get terminated) before it finishes its job. You can check how much longer your application can run by checking backgroundTimeRemaining property of the application object.

MHC
  • 6,405
  • 2
  • 25
  • 26
  • Yes, but thats not what I want. I want to have my app called everytime the user trigger the notification. – Abras Feb 08 '11 at 18:31
  • Exactly. I'm saying there is no way of doing it, well, as far as I can tell. – MHC Feb 08 '11 at 20:59
  • Yeah, thanks for the advice. I think my option will be implement my own iPod and build it into my app. It wont be to user friendly, but at least I can achieve what I want. – Abras Feb 09 '11 at 03:11