1

I'm doing this test app where I want to receive notification when the iPod changes the now playing item (song), the test is working nice while app is in foreground but as soon as the app goes to the background it stop getting notifications which is OK, when I tap on the app again (comes to foreground) I get all notifications according to all the times the now playing changed while the app was in background but everytime I'm getting the same song information, so how can I get the correct song information for each notification?

This is the test I did, in the AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    MPMusicPlayerController *player = [MPMusicPlayerController iPodMusicPlayer];

    [notificationCenter addObserver:self
                           selector:@selector(nowPlayingItemChanged:)
                               name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
                             object:player];

    [player beginGeneratingPlaybackNotifications];

    return YES;
}

-(void) nowPlayingItemChanged:(NSNotification *)notification {
    MPMusicPlayerController *player = (MPMusicPlayerController *)notification.object;

    MPMediaItem *song = [player nowPlayingItem];

    if (song) {
        NSString *title = [song valueForProperty:MPMediaItemPropertyTitle];
        NSString *album = [song valueForProperty:MPMediaItemPropertyAlbumTitle];
        NSString *artist = [song valueForProperty:MPMediaItemPropertyArtist];
        NSString *playCount = [song valueForProperty:MPMediaItemPropertyPlayCount];

        NSLog(@"title: %@", title);
        NSLog(@"album: %@", album);
        NSLog(@"artist: %@", artist);
        NSLog(@"playCount: %@", playCount);
    }
}
Geykel
  • 1,888
  • 1
  • 13
  • 15

2 Answers2

1

See this post your options in the background are pretty restricted:

StackOverFlow Post

And the Apple Docs regarding that state it is not really possible: Apple Documentation on Background states

Community
  • 1
  • 1
Cliff Ribaudo
  • 8,932
  • 2
  • 55
  • 78
  • Yes, I know the limitations, I don't want to do anything in the background, I just want to process the queued notifications which I'm getting fine but when I retrieve the nowPlayingItem from the player, it is always the same item? what I want to know is if there is a way to get the real item (the real song playing) for each notification... – Geykel Oct 08 '12 at 16:19
  • If you look at the documentation link I posted you will see that it does not appear possible. – Cliff Ribaudo Oct 09 '12 at 10:05
-1

Be sure to remove the observer when going into the background:

[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:musicPlayer];[player endGeneratingPlaybackNotifications];

Add it again when entering the foreground.

asklausen
  • 111
  • 1
  • 7