28

I'm working to update the MPNowPlayingInfoCenter and having a bit of trouble. I've tried quite a bit to the point where I'm at a loss. The following is my code:

    self.audioPlayer.allowsAirPlay = NO;

    Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");

    if (playingInfoCenter) {

        NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];

        MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"series_placeholder"]];

        [songInfo setObject:thePodcast.title forKey:MPMediaItemPropertyTitle];
        [songInfo setObject:thePodcast.author forKey:MPMediaItemPropertyArtist];
        [songInfo setObject:@"NCC" forKey:MPMediaItemPropertyAlbumTitle];
        [songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];

        [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];


    }

This isn't working, I've also tried:

   [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nil];

In an attempt to get it to remove the existing information from the iPod app (or whatever may have info there). In addition, just to see if I could find out the problem, I've tried retrieving the current information on app launch:

  NSDictionary *info = [[MPNowPlayingInfoCenter defaultCenter] nowPlayingInfo];
  NSString *title = [info valueForKey:MPMediaItemPropertyTitle];
  NSString *author = [info valueForKey:MPMediaItemPropertyArtist];

  NSLog(@"Currently playing: %@ // %@", title, author);

and I get Currently playing: (null) // (null)

I've researched this quite a bit and the following articles explain it pretty thoroughly, however, I am still unable to get this working properly. Am I missing something? Would there be anything interfering with this? Is this a service something my app needs to register to access (didn't see this in any docs)?

Apple's Docs

Change lock screen background audio controls

Now playing info ignored

Community
  • 1
  • 1
Jayson Lane
  • 2,828
  • 1
  • 24
  • 39
  • 1
    Is media playing when this code executes? – Ravin Mar 01 '12 at 19:32
  • I've tried with audio playing (via MPMoviePlayer) and without with the same results – Jayson Lane Mar 01 '12 at 19:44
  • print [MPNowPlayingInfoCenter defaultCenter] using nslog. In documentation they say : The default now playing info center holds now-playing information for the app that is designated as the receiver for remote-control events. Are you playing media on some remote device? make sure media is playing on some remote device. Because docs says : Remote-control events let users control application multimedia through the system transport controls or through an external accessory. – Ravin Mar 01 '12 at 20:00
  • When I print MPNowPlayingInfoCenter I get: – Jayson Lane Mar 01 '12 at 20:09
  • I'm not playing to a remote device or accessory... my understanding is that you can update this information (as displayed on the lock screen) simply by playing audio on the device but that it can also be used for remote devices... – Jayson Lane Mar 01 '12 at 20:19

2 Answers2

55

I finally figured out the problem, I was not prompting my app to receive remote control events, simply adding this line fixed the problem:

 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
Jayson Lane
  • 2,828
  • 1
  • 24
  • 39
  • I face a similar issue. Even though I implement all the above methods, i am not receiving control events. http://stackoverflow.com/questions/43973602/unable-to-receive-remotecontrolreceivedwithevent-objective-c-ios – A_G May 15 '17 at 10:07
  • Also, make sure you call `UIApplication.shared.beginReceivingRemoteControlEvents()` _after_ you've setup the audio session. The following worked for me: ```swift try? AVAudioSession.sharedInstance().setCategory(.playback) try? AVAudioSession.sharedInstance().setActive(true) UIApplication.shared.beginReceivingRemoteControlEvents() ``` – CMash Jul 23 '22 at 22:33
2

I use the code below and it always works. I'm also using MPMoviePlayer like you. Have you checked whether NSClassFromString(@"MPNowPlayingInfoCenter") ever actually returns YES? Have you set you app play audio in background key in your plist?

- (void) loadMPInformation
{
    NSDictionary *mpInfo;

    if([savedTrack.belongingAlbum.hasAlbumArt boolValue] == NO){
        mpInfo = [NSDictionary dictionaryWithObjectsAndKeys:savedTrack.belongingAlbum.album, MPMediaItemPropertyAlbumTitle, 
                  savedTrack.belongingArtist.artist, MPMediaItemPropertyArtist, savedTrack.name, MPMediaItemPropertyTitle, nil];   
    } else {
        UIImage *artImage = [UIImage imageWithData:savedTrack.belongingAlbum.art];
        MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:artImage];
        mpInfo = [NSDictionary dictionaryWithObjectsAndKeys:savedTrack.belongingAlbum.album, MPMediaItemPropertyAlbumTitle, 
                  savedTrack.belongingArtist.artist, MPMediaItemPropertyArtist, savedTrack.name, MPMediaItemPropertyTitle, artwork, MPMediaItemPropertyArtwork, nil];
    }
    [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = mpInfo;

}
deleterOfWorlds
  • 552
  • 5
  • 9
  • Yeah, I've checked that it returns YES, I've also set that the app play audio in the plist... – Jayson Lane Mar 01 '12 at 20:09
  • 3
    Wait. Have you tried this on a device or on the simulator? Cause it never works in the simulator I've noticed. – deleterOfWorlds Mar 01 '12 at 20:17
  • I've only been trying it on the device... do you have to register to receive notification from the play/pause button? – Jayson Lane Mar 01 '12 at 20:19
  • I did. But I don't remember it being necessary--good thing to try though. Also, I remember `MPNowPlayingInfoCenter` being pretty picky that no strings you send it are nil, be sure to test that too. – deleterOfWorlds Mar 01 '12 at 20:23
  • Yeah, none of the strings are nil – Jayson Lane Mar 01 '12 at 20:26
  • Also, have your thrown in: `[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; [[AVAudioSession sharedInstance] setActive: YES error: nil];` – deleterOfWorlds Mar 01 '12 at 20:30
  • yeah, and I've got error checking on that just in case and the session is being set properly – Jayson Lane Mar 01 '12 at 20:31
  • All I can say is register to receive notifications--might be required on your platform. Otherwise I dunno, the code should work. Is this iPhone? – deleterOfWorlds Mar 01 '12 at 20:35