How to set audio details with an image on control

2019-07-12 20:14发布

I am trying to display audio details on control center as shown in the screenshot attached.

With the below code it is working as expected in all the versions of iOS except in iOS11:

if ([MPNowPlayingInfoCenter class])  {
    NSString *progress = (appDelegate.audioPlayer.progress == 0 || appDelegate.audioPlayer == nil ) ? @"0.0" : [NSString stringWithFormat:@"%f",appDelegate.audioPlayer.progress];
    NSString *duration = (appDelegate.audioPlayer.progress == 0 || appDelegate.audioPlayer == nil ) ? @"0.0" :[NSString stringWithFormat:@"%f",appDelegate.audioPlayer.duration];
    UIImage *image;
    if (appDelegate.selectedImg.length == 0 || appDelegate.selectedImg == nil) {
        image = [UIImage imageNamed:@"Music_Logo_1_"];
    }
    else{
        NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:appDelegate.selectedImg]];
        image = [UIImage imageWithData:data] != nil ? [UIImage imageWithData:data] : [UIImage imageNamed:@"Music_Logo_1_"];
    }
    NSDictionary *currentlyPlayingTrackInfo = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:appDelegate.selectedTitle != nil && ![appDelegate.selectedTitle isEqualToString:@""] ? appDelegate.selectedTitle : @"", appDelegate.selecteddesc != nil && ![appDelegate.selecteddesc isEqualToString:@""] ? appDelegate.selecteddesc : @"", progress , duration,[[MPMediaItemArtwork alloc] initWithImage:image],@"Music", nil] forKeys:[NSArray arrayWithObjects:MPMediaItemPropertyTitle,MPMediaItemPropertyAlbumTitle, MPNowPlayingInfoPropertyElapsedPlaybackTime ,MPMediaItemPropertyPlaybackDuration, MPMediaItemPropertyArtwork,MPMediaItemPropertyArtist, nil]];
    [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = currentlyPlayingTrackInfo;
}

MPNowPlayingInfoCenter properties are not working for iOS 11 which is working as expected in lower versions of iOS.

Output in iOS 11:

This actually displayed in iOS11

Expected Result:

Expected output

0条回答
登录 后发表回答