How to set audio details with an image on control center in Objective-C for iOS11?

[亡魂溺海] 提交于 2019-12-24 03:03:59

问题


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:

Expected Result:

来源:https://stackoverflow.com/questions/47285207/how-to-set-audio-details-with-an-image-on-control-center-in-objective-c-for-ios1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!