0

I am working on the music player app in which song info is to be displayed in lockscreen when the phone gets locked, I have added the following method in

- (void)applicationWillResignActive:(UIApplication *)application of appdelegate

-(void)setTalkPlayerTrackInfoForLockScreenPlayer
{

Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");

if (playingInfoCenter)
{

//    Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");

//    if (playingInfoCenter){

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



NSString *albumArtImageURL = [[ShowDelegate sharedInstance].programImage objectAtIndex:[ShowDelegate sharedInstance].selectedIndex];

NSURL *urls = [NSURL URLWithString:albumArtImageURL];




NSData *imageData = [NSData dataWithContentsOfURL:urls];

UIImage *imgToLockScreen;
if (imageData != nil) {
    imgToLockScreen = [UIImage imageWithData:imageData];
}
else {
    imgToLockScreen = [UIImage imageNamed:@"rlogo.png"];
}

MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:imgToLockScreen];

NSNumber *duration = [NSNumber numberWithDouble:[[ShowDelegate sharedInstance].audioStreamer duration]];
NSNumber *currentTime = [NSNumber numberWithDouble:[[ShowDelegate sharedInstance].audioStreamer currentTime]];
NSNumber *playbackRate = [NSNumber numberWithDouble:1.0];


NSString *trackTitle = [[ShowDelegate sharedInstance].podtitle objectAtIndex:[ShowDelegate sharedInstance].selectedIndex];
NSString *programName = [[ShowDelegate sharedInstance].programname objectAtIndex:[ShowDelegate sharedInstance].selectedIndex];


NSString *trackTitle2  = [trackTitle stringByRemovingPercentEncoding];

NSString  *sterg = [trackTitle2  stringByReplacingOccurrencesOfString:@"+" withString:@" "];


NSString *trackTitle1  = [programName stringByRemovingPercentEncoding];
    NSString  *sterg1 = [trackTitle1  stringByReplacingOccurrencesOfString:@"+" withString:@" "];








if (trackTitle != NULL) {
    [songInfo setObject:sterg forKey:MPMediaItemPropertyTitle];
    [songInfo setObject:sterg1 forKey:MPMediaItemPropertyArtist];
    [songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
    [songInfo setObject:duration forKey:MPMediaItemPropertyPlaybackDuration];
    [songInfo setObject:playbackRate forKey:MPNowPlayingInfoPropertyPlaybackRate];
    [songInfo setObject:currentTime forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
}


[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
    }
}

Now the song info is displayed in lock screen in simulator but not in real device in os 10.2.I have also added [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; Please help if anyone knows what I am doing wrong ?

Dinesh Kumar
  • 165
  • 1
  • 14
  • 1
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [self becomeFirstResponder]; //Have you add this in Viewdidappear method – Lalit kumar Jul 12 '17 at 12:08
  • @Lalit kumar I have added the code in AppDelegate, So I have another class method called ShowDelegate in which I store information from player view controller .So should I add these code in player view controller? – Dinesh Kumar Jul 12 '17 at 12:16

0 Answers0