Search code examples
iosavfoundationmpnowplayinginfocenter

Get meta data displayed in MPNowPlayingInfoCenter's nowPlayingInfo(lock screen and remote control)


Thanks for noticing this question. I want to do something about music recommendation, and what I am doing now is leveraging MPNowPlayingInfoCenter's nowPlayingInfo, like this:

NSDictionary *metaData = [[MPNowPlayingInfoCenter defaultCenter] nowPlayingInfo];
NSString *songTitle = metaData[MPMediaItemPropertyTitle];
NSString *albumnTitle = metaData[MPMediaItemPropertyAlbumTitle];
NSString *artist = metaData[MPMediaItemPropertyArtist];

But it always returns nil when "Music" app is playing music in background. I looked up the related documents, it says

 MPNowPlayingInfoCenter provides an interface for setting the current now 
 playing information for the application.
 The default center holds now playing info about the current application

Seems there is no way to get other app's nowPlayingInfo through MPNowPlayingInfoCenter. So are there any other ways to get other app's music meta data displayed in remote control/lock screen? Thanks!


Solution

  • You can get what iPod currently is playing

    MPMusicPlayerController* player = [MPMusicPlayerController iPodMusicPlayer];
    //get now playing item
    MPMediaItem*item = [player nowPlayingItem];
    // get the title of song
    NSString* titleStr = [item valueForProperty:MPMediaItemPropertyTitle];
    NSLog(@"titlestr %@",titleStr);