How to set an title of the currently playing audio in iPhone lock screen?
Asked Answered
G

1

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?

Gladiate answered 5/12, 2011 at 15:2 Comment(0)
S
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;
}
Sierra answered 5/12, 2011 at 15:10 Comment(6)
No there is not, since this api is only available in iOS 5. Prior to iOS there is no API to achieve this.Sierra
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.Prominent
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.Sierra
in iOS4 I am getting the following error! "adyld: Symbol not found: _OBJC_CLASS_$_MPNowPlayingInfoCenter". How can I remove this crash.Prominent
Have you implemented ti code above with the if statement to check if the class is available ?Sierra
Remember to go to Build Phases->Link Binary With Libraries and add MediaPlayer.framework. Also, add #import <MediaPlayer/MediaPlayer.h>.Nottinghamshire

© 2022 - 2024 — McMap. All rights reserved.