iOS receive Media Player Notifications when app is in background
Asked Answered
T

1

4

Probably a simple question - can an iOS app continue to receive media player notifications [NSNotifications] (i.e. MPMusicPlayerControllerNowPlayingItemDidChangeNotification) once it moves into background?

I'm specifically using an iPodMusicPlayer to play songs over my main app functionality, and id like to play about with implementing code under this notification after the app falls into background mode.

Not asking for code; just a point in the right direction re: background media notifications ;)

Tesler answered 20/11, 2012 at 23:19 Comment(0)
B
2

I have made some checks since i need the same.

Unfortunately I have seen that your app does not receive notification whilst in background.

However, I realized that it does receive the entire notification queue once resumed. The problem is that notification are received all together giving you no information about the time and date they were fired up.

Of course, since you receive them all at once, even if you try to save the nowPlayingItem after the MPMusicPlayerControllerNowPlayingItemDidChangeNotification, you will only see the current item when your app becomes active again, and nothing more. Unfortunately, I think we cannot do anything more, since Apple doc also says

The notification has no userInfo dictionary

meaning that you cannot get any info out of the notification.

Balthazar answered 23/11, 2012 at 15:13 Comment(5)
I've actually found a solution (or more accurately, a work around) for this: I simply used the didenterbackground method in appdelegate class to request the maximum extra time allowed (I think 500 seconds). This allows the notification to fire if it is within this 500 second window (which most songs are!). :)Tesler
Ok, but what kind of operation do you perform during that extra time? Can you just "do nothing" for 500 seconds?Balthazar
i discovered the beginBackgroundTaskWithExpirationHandler method. Luckily, this allows any notifications (in my case the MPMusicPlayerControllerNowPlayingItemDidChangeNotification) to still fire during this time, and subsequent methods ive coded are triggered following it. I will did out the article detailing this method.....Tesler
Hi can you post your solution for this please?Chiliad
Has anyone figured out a solution to this problem? Does the 500 second timer reset itself when a song changes? Otherwise it wont be enough.Dx

© 2022 - 2024 — McMap. All rights reserved.