9

When you play a music, the music title is shown below the time in the lock screen.

I have also seen how TuneIn radio does that by showing the name of the currently playing radio station.

How do you do that?

James Webster
  • 31,873
  • 11
  • 70
  • 114
samwize
  • 25,675
  • 15
  • 141
  • 186

1 Answers1

27

Read the documentation: MPNowPlayingInfoCenter

And here is an example code that will work on iOS 5 and it will not crash on older versions of iOS.

Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");

if (playingInfoCenter) {
    MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
    NSDictionary *songInfo = [NSDictionary dictionaryWithObjectsAndKeys:
                             @"Some artist", MPMediaItemPropertyArtist,
                             @"Some title", MPMediaItemPropertyTitle,
                             @"Some Album", MPMediaItemPropertyAlbumTitle,
                             nil];
    center.nowPlayingInfo = songInfo;
}
rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • No there is not, since this api is only available in iOS 5. Prior to iOS there is no API to achieve this. – rckoenes Jan 03 '12 at 07:38
  • It works well for ios5, but giving crash on older version (e.g ios4). Is there any alternative for ios4? If yes then please help me. Thanks in advance. – Khalid Usman May 29 '12 at 11:37
  • The code as implement above will not crash on iOS4, but will not work in iOS since iOS4 does nto support it. There is no way to get the song info on the lock screen in iOS4. – rckoenes May 29 '12 at 11:53
  • in iOS4 I am getting the following error! "adyld: Symbol not found: _OBJC_CLASS_$_MPNowPlayingInfoCenter". How can I remove this crash. – Khalid Usman Jun 13 '12 at 07:52
  • Have you implemented ti code above with the if statement to check if the class is available ? – rckoenes Jun 13 '12 at 07:54
  • Remember to go to Build Phases->Link Binary With Libraries and add MediaPlayer.framework. Also, add #import . – Szuwar_Jr Mar 23 '17 at 08:21